gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:43:22 31 4
gpt4 key购买 nike

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com