使用的System.Uri去除多余的斜杠 [英] Using System.Uri to remove redundant slash

查看:1800
本文介绍了使用的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/");

这篇关于使用的System.Uri去除多余的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆