为什么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.
更新:
@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
,它也会失败。
发布于 2011-11-13 13:06:13
文件uri方案(RFC 1738) file://host/path显示主机是可选的。/test.html意味着“由于通常用于本地文件,因此RFC 1738的主机通常是空的,从而导致启动三重/. (ref)”。
将file:///test.htm /test.htm改为,URI构造函数将正确地解析它。AbsolutePath
将是/test.html。
希望这能有所帮助。
发布于 2011-11-13 13:00:10
我认为构造函数正在将"//test.htm"解释为没有方案和test.htm主机名的URI。您可以通过检查"http://test.htm/".的secondRelativeUri
值来看到这一点
您正在创建的第三个URI无效,因为您有太多的斜杠。
发布于 2011-11-13 13:51:11
新Uri(googleU,"//test.htm")意味着Uri = http://test.html/ /*有效,无论如何,在某个*/
新Uri(googleU,“/test.htm”)意味着Uri = http:///test.html/ /*无效,无意义*/
新Uri (“//test.htm”,UriKind.Relative);//=> Uri=/test.htm相同,而不是相对位置
var r=新Uri("test.htm",UriKind.Relative);
新Uri(googleU,r);// => Uri = http://www.google.com/test.htm
https://stackoverflow.com/questions/8111566
复制相似问题