Uri 构造函数 .NET Framework 错误?

发布于 2024-12-14 23:22:18 字数 655 浏览 0 评论 0原文

为什么第三个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 技术交流群。

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

发布评论

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

评论(4

ペ泪落弦音 2024-12-21 23:22:18

文件 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.

你不是我要的菜∠ 2024-12-21 23:22:18

我认为构造函数将 "//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.

花落人断肠 2024-12-21 23:22:18

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

我还不会笑 2024-12-21 23:22:18

即使在创建相对 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.

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