Uri.IsWellFormedUriString 需要更新吗?
我想我可能在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须更改配置才能支持 RFC 3986 和 RFC 3987。
这是您必须进行的配置:
取自此处 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:
Taken from here http://msdn.microsoft.com/en-us/library/system.uri.aspx#CodeSnippetContainerCode5