Uri.IsWellFormedUriString 需要更新吗?

发布于 2024-11-09 15:51:34 字数 1571 浏览 0 评论 0原文

我想我可能在 Uri.IsWellFormedUriString 方法中发现了一个错误,这可能是因为它只符合 RFC 2396 和 RFC 2732 标准,而不是较新的 RFC 3986 使上述两个内容过时。

我认为发生的情况是任何非 us-ascii 字符都会使其失败,因此包含 æ、ø、ö 或 å 等字符的 url 会使其返回 false。由于现在允许使用这些类型的字符(维基百科等都使用它们)我认为 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

I think I might have discovered an error in the Uri.IsWellFormedUriString method, it might be because it only conforms to the RFC 2396 and RFC 2732 standards and not the newer RFC 3986 which makes the two aforementioned obsolete.

What I think happens is that any non us-ascii characters makes it fail, so urls with characters like æ, ø, ö or å in it will make it return false. Since these kind of characters are now allowed (wikipedia among others uses them) I think Uri.IsWellFormedUriString should accept them. The regex below is taken from RFC 3986.

What do you think? Should the Uri class be updated?

Anyway here's some sample code which shows the error:

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("");
            }
}

Output:

/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-11-16 15:51:34

您必须更改配置才能支持 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

You have to change you configuration to support the RFC 3986 and RFC 3987.
This is the config you have to make:

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

Taken from here http://msdn.microsoft.com/en-us/library/system.uri.aspx#CodeSnippetContainerCode5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文