【问题标题】:Uri.IsWellFormedUriString needs to be updated?Uri.IsWellFormedUriString 需要更新吗?
【发布时间】:2011-08-31 17:54:35
【问题描述】:

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

我认为发生的情况是,任何非 us-ascii 字符都会使其失败,因此其中包含 æ、ø、ö 或 å 等字符的 url 将使其返回 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

【问题讨论】:

标签: c# .net regex uri


【解决方案1】:

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

【讨论】:

  • 这很尴尬,我只看了 Uri.IsWellFormedUriString 的文档 :-) 谢谢!
  • 你的权利:-)。我还没能使它工作。当我在运行时检查 Uri 类时,s_IdnScope 和 s_IriParsing 仍然有错误的值,并且 Uri.IsWellFormedUriString 仍然不接受 iri url。
  • @Simon 你确定你的配置在正确的项目中吗?它必须在调用项目中
  • 是的,我检查过了,我什至使用了 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 来检查它使用了哪个文件,并且它是包含设置的正确文件。
  • @Oskar 你能用我提供的测试代码让它工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
相关资源
最近更新 更多