【发布时间】:2011-11-13 12:37:52
【问题描述】:
为什么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,它也会失败
【问题讨论】:
-
为什么你通过“///test.htm”而不是“//test.htm”??
-
@DavidePiras 因为我很邪恶?我需要提出以下请求:example.com///test.htm 在一个完美的世界中 example.com/test.htm 和 example.com///test.htm 并没有什么不同,但不幸的是,真正的 Web 服务器和框架很疯狂,而且有数千个其中。
-
我同意@dr.evil。解析应该接受无用的目录/路径分隔符。