使用的System.Uri去除多余的斜杠

使用的System.Uri去除多余的斜杠

问题描述:

我在程序中的条件在那里我有一个服务器整合(如 HTTP://server1.my.corp/ ),可能会或可能不会有一个使用相对路径斜杠结尾(例如: /应用/ TestOne / )。按照文档乌里应该......

I have a condition in my program where I have to combine a server (e.g. http://server1.my.corp/) that may or may not have an ending slash with a relative path (e.g. /Apps/TestOne/). According to the docs, Uri should...

Canonicalizes通过压缩,如/./序列进行分层URI路径, /../,//,...

Canonicalizes the path for hierarchical URIs by compacting sequences such as /./, /../, //,...

所以,当我做类似 VAR URL =新乌里(服务器+ relativePath),我希望它采取什么本来是 HTTP://server1.my.corp//Apps/TestOne/ ,然后取出双斜杠(即 // - > / ),但的ToString AbsolutePath 和各种选项仍然显示冗余/重复的斜线。我不能使用乌里吧?

So when I do something like var url = new Uri(server + relativePath), I'd expect it to take what would otherwise be http://server1.my.corp//Apps/TestOne/ and remove the double slash (i.e // -> /), but ToString, AbsolutePath and various options still show the redundant/duplicate slash. Am I not using Uri right?

看看的构造函数href=\"http://msdn.microsoft.com/en-us/library/system.uri.aspx\" rel=\"nofollow\"> Uri类的

Take a look at the constructors for the Uri class. You need to specify a base Uri and a relative path to get the canonized behavior. Try something like this:

var server = new Uri("http://server1.my.corp/");
var resource = new Uri(server, "/Apps/TestOne/");