Gun Gun - 4 months ago 21
HTML Question

c# Can't match Html Special characters

I have to match 2 urls, the first one comes from MySQL db and the second one comes from an Html page. If I compare both as string

var match = Regex.Match(href.Attributes["href"].Value, testString, RegexOptions.IgnoreCase);


the
match.Success = false.
Both strings are like
this : myUrl/rollcontainer-weiß
but the match.Success is still false.

I tried to add
HttpUtility.HtmlEncode
to check both strings and I get :
myUrl/rollcontainer-wei&#233
for the first one and
myUrl/rollcontainer-wei&ß
for the second one.

How can I have a
match.Success = true
in this case ?

Answer

Try this function, for example.

static void Main(string[] args)
{
    bool test = Test("http://myUrl.com/rollcontainer-Wei&ß", "http://myUrl.com/rollcontainer-wei&ß");

}

public static bool Test(string url1, string url2)
{
    Uri uri1 = new Uri(HttpUtility.HtmlDecode(url1)); 
    Uri uri2 = new Uri(HttpUtility.HtmlDecode(url2));

    var result = Uri.Compare(uri1, uri2,
        UriComponents.Host | UriComponents.PathAndQuery,
        UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase);

    return result == 0;
}
Comments