Uri.IsWellFormedUriString需要更新吗?

时间:2011-05-24 08:23:46

标签: c# .net regex uri

我想我可能在Uri.IsWellFormedUriString方法中发现了一个错误,可能是因为它只符合RFC 2396RFC 2732标准,而不是更新的RFC 3986前面提到的两个已经过时了。

我认为发生的是,任何非us-ascii字符都会导致失败,因此其中包含æ,ø,ö或å等字符的网址会使其返回false。由于现在允许这些类型的字符(wikipedia使用它们),我认为Uri.IsWellFormedUriString应该接受它们。下面的正则表达式取自RFC 3986。

你怎么看? Uri课程应该更新吗?

无论如何,这里有一些显示错误的示例代码:

static void Main(string[] args)
        {
            var urls = new []
                           {
                               @"/aaa/bbb/cccd",
                               @"/aaa/bbb/cccæ",
                               @"/aaa/bbb/cccø",
                               @"/aaa/bbb/cccå"
                           };

            var regex = new Regex(@"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?");

            Debug.WriteLine("");

            foreach (var url in urls)
            {
                if (Uri.IsWellFormedUriString(url, UriKind.Relative))
                    Debug.WriteLine(url + " is a wellformed Uri");

                if (regex.IsMatch(url))
                    Debug.WriteLine(url + " passed the Regex");

                Debug.WriteLine("");
            }
}

输出:

/aaa/bbb/cccd is a wellformed Uri
/aaa/bbb/cccd passed the Regex

/aaa/bbb/cccæ passed the Regex

/aaa/bbb/cccø passed the Regex

/aaa/bbb/cccå passed the Regex

1 个答案:

答案 0 :(得分:12)

您必须更改配置以支持RFC 3986和RFC 3987。 这是你必须做的配置:

<configuration>
  <uri>
  <idn enabled="All" />
  <iriParsing enabled="true" />
  </uri>
</configuration>

从这里采取http://msdn.microsoft.com/en-us/library/system.uri.aspx#CodeSnippetContainerCode5

相关问题