从基本URI和相对路径创建新URI - 斜杠有何不同?

时间:2014-03-20 19:55:22

标签: c# .net uri trailing-slash

为什么在使用new URI(baseUri, relativePath)时斜杠有所不同?

  

此构造函数通过组合baseUri和relativeUri ..

来创建一个Uri实例

并且 如何将相对路径安全/一致地附加到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没有尾部斜杠也是如此。

3 个答案:

答案 0 :(得分:16)

  

为什么斜杠在使用新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。合并路径

     

上面的伪代码是指用于合并a的“合并”例程      相对路径引用与基URI的路径。这是      完成如下:

     
      
  • 如果基URI具有已定义的权限组件且为空     path,然后返回一个由“/”连接的字符串     参考路径;否则,

  •   
  • 返回由引用的路径组件组成的字符串     附加到除基本URI路径的最后一段之外的所有部分(即,     排除基URI中最右边的“/”之后的任何字符     路径,或者如果不包含整个基本URI路径,则将其排除     任何“/”字符。

  •   

答案 1 :(得分:8)

我一直在使用带有重载new Uri(baseUri, relativePath)的Uri构造函数。也许其他人可能会发现结果很有用。这是我写的测试应用程序的输出:

A) Base Address is domain only
==============================

NO trailing slash on base address, NO leading slash on relative path:
http://foo.com   +  relative1/relative2 :
    http://foo.com/relative1/relative2

NO trailing slash on base address, relative path HAS leading slash:
http://foo.com   +  /relative1/relative2 :
    http://foo.com/relative1/relative2

Base address HAS trailing slash, NO leading slash on relative path:
http://foo.com/   +  relative1/relative2 :
    http://foo.com/relative1/relative2

Base address HAS trailing slash, relative path HAS leading slash:
http://foo.com/   +  /relative1/relative2 :
    http://foo.com/relative1/relative2

B) Base Address includes path
=============================

NO trailing slash on base address, NO leading slash on relative path:
http://foo.com/base1/base2   +  relative1/relative2 :
    http://foo.com/base1/relative1/relative2 
    (removed base2 segment)

NO trailing slash on base address, relative path HAS leading slash:
http://foo.com/base1/base2   +  /relative1/relative2 :
    http://foo.com/relative1/relative2
    (removed base1 and base2 segments)

Base address HAS trailing slash, NO leading slash on relative path:
http://foo.com/base1/base2/   +  relative1/relative2 :
    http://foo.com/base1/base2/relative1/relative2
    (has all segments)

Base address HAS trailing slash, relative path HAS leading slash:
http://foo.com/base1/base2/   +  /relative1/relative2 :
    http://foo.com/relative1/relative2
    (removed base1 and base2 segments)

答案 2 :(得分:0)

我一直在寻找相同的商品,并得出以下解决方案:

var badBase = new Uri("http://amee/noTrailingSlash");
var goodBase = new Uri("http://amee/trailingSlash/");
var f = "relPath";
string badBaseUrl = Path.Combine(badBase,f);
string goodBaseUrl = Path.Combine(goodBase,f);
new Uri(badBaseUrl);  //----> (http://amee/trailingSlash/relPath)
new Uri(goodBaseUrl); //---> (http://amee/trailingSlash/relPath)