Uri.IsWellFormedUriString需要更新? [英] Uri.IsWellFormedUriString needs to be updated?

查看:498
本文介绍了Uri.IsWellFormedUriString需要更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可能已经发现了Uri.IsWellFormedUriString方法错误,可能是因为它不仅符合的 RFC 2396 RFC 2732 标准,而不是新的< A HREF =htt​​p://www.ietf.org/rfc/rfc3986.txt相对=nofollow> RFC 3986 这使得上述两个过时。

我认为情况是,任何非US-ASCII字符使得它失败了,所以网址与像æ人物,O,或在它将使返回false。由于这些类型的字符,现在允许(维基等等使用它们),我认为Uri.IsWellFormedUriString应该接受他们。下面的正则表达式取自RFC 3986。

你怎么看?如果Uri类更新?

反正这里的一些样品code这显示了错误:

 静态无效的主要(字串[] args)
        {
            VAR的网址=新的[]
                           {
                               @/ AAA / BBB / CCCD
                               @/ AAA / BBB /cccæ
                               @/ AAA / BBB /cccø
                               @/ AAA / BBB / CCCA
                           };

            VAR正则表达式=新的正则表达式(@^(([^:/#] +):)(//([^ /#?] *))([^#?] *)(\([??? ^#] *))(#(*)))??;

            的Debug.WriteLine();

            的foreach(URL中VAR URL)
            {
                如果(Uri.IsWellFormedUriString(URL,UriKind.Relative))
                    的Debug.WriteLine(网址+是一个简洁(wellformed)乌里);

                如果(regex.IsMatch(URL))
                    的Debug.WriteLine(网址+,通过了正则表达式);

                的Debug.WriteLine();
            }
}
 

输出:

  / AAA / BBB / CCCD是一个简洁(wellformed)乌里
/ AAA / BBB / CCCD通过正则表达式

/ AAA / BBB /cccæ通过正则表达式

/ AAA / BBB /cccø通过正则表达式

/ AAA / BBB / CCCA通过正则表达式
 

解决方案

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

 &lt;结构&GT;
  &LT; URI&GT;
  &LT; IDN启用=全部/&GT;
  &LT; iriParsing启用=真/&GT;
  &LT; / URI&GT;
&LT; /结构&gt;
 

在这里http://msdn.microsoft.com/en-us/library/system.uri.aspx#$c$cSnippetContainer$c$c5

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

解决方案

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

这篇关于Uri.IsWellFormedUriString需要更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆