c# - 从基本 URI 和相对路径创建新的 URI - 斜线有区别吗?

标签 c# .net uri trailing-slash

为什么在使用 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/

相关文章:

c# - 具有多个条件的 while 语句

c# - 如何在 C++ 中调用从 C# 返回字符串的函数?

.net - 重定向时将数组或字符串列表从一个操作传递到另一个操作

apk - 在 Android 7/api24 中以编程方式安装 apk

PHP 获取 URL 的 URI 部分

c# - 如何在winrt中解压SharpZipLib压缩的字符串?

c# - MSMQ 用于持久性?

c# - ASP.NET 中的 SqlDependency

c# - 有没有办法使 View 状态过期?

java - Java 8中basic和url base64编码的区别