Uri 构造函数 .NET Framework 错误?
为什么第三个RelativeUri 失败?这是 .NET 错误吗? 4.0 好像也没有修复。
var googleU = new Uri("http://www.google.com");
var secondRelativeUri = new Uri(googleU,"//test.htm"); // doesn't fail
var thirdRelativeUri = new Uri(googleU,"///test.htm"); // fails - Invalid URI: The hostname could not be parsed.
更新:
@dariom 指出这是因为 .NET 中的协议相对 URL 处理是有意义的,但这对我来说仍然是错误的:
var thirdRelativeUri = new Uri("///test.htm",UriKind.Relative); // works as expected
var newUri = new Uri(googleU,thirdRelativeUri); //Fails, same error even though it's a relative URI
即使第二个 Uri 是Relative
Why the thirdRelativeUri fails? Is this a .NET bug? Seems like not fixed in 4.0 either.
var googleU = new Uri("http://www.google.com");
var secondRelativeUri = new Uri(googleU,"//test.htm"); // doesn't fail
var thirdRelativeUri = new Uri(googleU,"///test.htm"); // fails - Invalid URI: The hostname could not be parsed.
UPDATE:
@dariom pointed out that this is because protocol relative URL handling in .NET which make sense however this still seems buggy to me:
var thirdRelativeUri = new Uri("///test.htm",UriKind.Relative); // works as expected
var newUri = new Uri(googleU,thirdRelativeUri); //Fails, same error even though it's a relative URI
It fails even when the second Uri is Relative
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文件 uri 方案 (RFC 1738) file://[host]/path 显示 host 是可选的。 ///test.html 的意思是“由于这通常用于本地文件,因此 RFC 1738 中的主机通常为空,导致起始三元组 /。 (ref)"
将 ///test.htm 更改为 file:///test.htm,URI 构造函数将解析它 适当地。它的
AbsolutePath
将是 /test.html。希望这有帮助。
The file uri scheme (RFC 1738) file://[host]/path shows that host is optional. ///test.html would mean "Since this usually used for local files the host from RFC 1738 is often empty leading to a starting triple /. (ref)"
Change ///test.htm to file:///test.htm and the URI constructor will parse it properly. It's
AbsolutePath
will be /test.html.Hope this helps.
我认为构造函数将 "//test.htm" 解释为没有方案且主机名为 test.htm 的 URI。您可以通过检查
secondRelativeUri
的值来查看这一点 - 它是“http://test.htm/”。您创建的第三个 URI 无效,因为斜杠太多。
I think that the constructor is interpreting "//test.htm" as a URI with no scheme and a hostname of test.htm. You can see this by examining the value of
secondRelativeUri
- it's "http://test.htm/".The third URI you're creating is invalid because you have too many slashes.
new Uri(googleU,"//test.htm") 表示 Uri = http://test.html/ /*有效,无论如何,某个根 */
new Uri(googleU,"///test.htm") 意味着 Uri = http:// /test.html/ /* 无效,无意义 */
new Uri("///test.htm",UriKind.Relative); //=> Uri = ///test.htm 同样的错误,不是相对位置
var r = new Uri("test.htm",UriKind.Relative);
新的 Uri(googleU, r); // => Uri = http://www.google.com/test.htm
new Uri(googleU,"//test.htm") mean Uri = http://test.html/ /* valid, Anyway a root somewhere */
new Uri(googleU,"///test.htm") mean Uri = http:///test.html/ /* invalid, Meaningless */
new Uri("///test.htm",UriKind.Relative); //=> Uri = ///test.htm same mistake, not relative location
var r = new Uri("test.htm",UriKind.Relative);
new Uri(googleU, r); // => Uri = http://www.google.com/test.htm
即使在创建相对 URL 时,.net 也会将以两个斜杠开头的字符串视为主机名,如“//example.org/document”中所示。同样,三个斜杠会造成混乱并引发异常。如果您非常确定这些 //test.htm 和 ///test.htm 是路径,那么您可以尝试使用 UriBuilder 类。
Even when creating relative URLs, .net treats a string that starts with tow slashes as a host name like in "//example.org/document". Similarly three slahes makes a confusion and an exception is thrown. If you are pretty sure these //test.htm and ///test.htm are paths, then you can try using UriBuilder class.