Uri构造函数.NET Framework错误?

dr.*_*vil 3 .net c# uri

为什么thirdRelativeUri失败了?这是一个.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.
Run Code Online (Sandbox Code Playgroud)

更新:

@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
Run Code Online (Sandbox Code Playgroud)

即使是第二个Uri,它也会失败 Relative

Bri*_*haw 5

文件uri方案(RFC 1738)file:// [host]/path显示主机是可选的.///test.html意味着"由于这通常用于本地文件,因此RFC 1738中的主机通常为空,从而导致启动三元组.(参考) "

///test.htm更改为file:///test.htm,URI构造函数将正确解析它.它将AbsolutePath/test.html.

希望这可以帮助.