[英]Using System.Uri to remove redundant slash
I have a condition in my program where I have to combine a server (eg http://server1.my.corp/
) that may or may not have an ending slash with a relative path (eg /Apps/TestOne/
). 我在我的程序中有一个条件,我必须组合一个服务器(例如http://server1.my.corp/
),它可能有或没有带有相对路径的结尾斜线(例如/Apps/TestOne/
)。 According to the docs , Uri
should... 根据文件 , Uri
应该......
Canonicalizes the path for hierarchical URIs by compacting sequences such as /./, /../, //,... 通过压缩序列(例如/./,/../,//,...)来规范化分层URI的路径
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 (ie //
-> /
), but ToString
, AbsolutePath
and various options still show the redundant/duplicate slash. 因此,当我执行类似var url = new Uri(server + relativePath)
,我希望它采取原本的http://server1.my.corp//Apps/TestOne/
并删除双斜杠(即//
- > /
),但ToString
, AbsolutePath
和各种选项仍显示冗余/重复斜杠。 Am I not using Uri
right? 我没有使用Uri
吗?
Take a look at the constructors for the Uri class . 看一下Uri类的构造函数。 You need to specify a base Uri and a relative path to get the canonized behavior. 您需要指定基本Uri和相对路径以获取标准化行为。 Try something like this: 尝试这样的事情:
var server = new Uri("http://server1.my.corp/");
var resource = new Uri(server, "/Apps/TestOne/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.