为什么在使用 new URI(baseUri, relativePath) 时斜杠会有所不同? ?
This constructor creates a Uri instance by combining the baseUri and the relativeUri ..
而且,如何将相对路径安全/一致地附加到 URI?
var badBase = new Uri("http://amee/noTrailingSlash");
var goodBase = new Uri("http://amee/trailingSlash/");
var f = "relPath";
new Uri(badBase, f) // BAD -> http://amee/relPath
new Uri(goodBase, f) // GOOD -> http://amee/trailingSlash/relPath
期望的输出是“好的”情况,即使初始 URI 没有尾部斜线也是如此。
最佳答案
Why does a slash make difference when using new URI(baseUri, relativePath)?
嗯,这就是网络上通常发生的事情。
例如,假设我正在查看 http://foo.com/some/file1.html
并且有一个指向 file2.html
的链接 - 该链接转到 http://foo.com/some/file2.html
,对吗?不是 http://foo.com/some/file1.html/file2.html
。
不过,更具体地说,这遵循 RFC 3986 的第 5.2.3 节.
5.2.3. Merge Paths
The pseudocode above refers to a "merge" routine for merging a relative-path reference with the path of the base URI. This is accomplished as follows:
If the base URI has a defined authority component and an empty path, then return a string consisting of "/" concatenated with the reference's path; otherwise,
return a string consisting of the reference's path component appended to all but the last segment of the base URI's path (i.e., excluding any characters after the right-most "/" in the base URI path, or excluding the entire base URI path if it does not contain any "/" characters).
关于c# - 从基本 URI 和相对路径创建新的 URI - 斜线有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543723/