c#无法匹配Html个特殊字符

c# Can't match Html Special characters

我必须匹配 2 个 url,第一个来自 MySQL 数据库,第二个来自 Html 页面。如果我将两者都比较为 string

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

match.Success = false. 两个字符串都像 this : myUrl/rollcontainer-weiß 但 match.Success 仍然是错误的。

我尝试添加 HttpUtility.HtmlEncode 来检查两个字符串,我得到:第一个字符串为 myUrl/rollcontainer-wei&#233,第二个字符串为 myUrl/rollcontainer-wei&ß

在这种情况下,我怎样才能得到 match.Success = true

试试这个功能,例如。

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;
}