如何结合的URI [英] How to combine URIs
问题描述
我有传递到一些code两个开放的对象,一个是目录,另一种是文件名(或相对路径)
VAR一个=新的URI(文件:/// C:/有/迪尔斯);
变种B =新的URI(some.file);
当我试图将它们结合起来是这样的:
变种C =新的URI(A,B);
我收到
文件:/// C:/Some/some.file
在这里我沃尔德期望得到同样的效果与 Path.Combine
(因为这是老code我需要更换):
文件:/// C:/Some/Dirs/some.file
我不能想到一个干净的解决方案这一点。
丑陋的解决方案是一个 /
添加到开放的,如果它不存在
字符串s = a.OriginalString;
如果(S [s.Length-1]!='/')
一个=新的URI(S +/);
那么,你将不得不告诉乌里的不知何故的是,最后一部分是一个目录,而不是一个文件。使用斜线似乎是最明显的方式给我。
记住,对许多尤里斯,你得到的答案是完全正确的。例如,如果您的Web浏览器的渲染
http://foo.com/bar/index.html
和它看到other.html相对链接它,然后去
http://foo.com/bar/other.html
不是
http://foo.com/bar/index.html/other.html
使用的目录斜线尤里斯是表明相对URI应该只是追加而不是替换的pretty的熟悉的方式。
I have two Uri objects passed into some code, one is a directory and the other is a filename (or a relative path)
var a = new Uri("file:///C:/Some/Dirs");
var b = new Uri("some.file");
when I try and combine them like this:
var c = new Uri(a,b);
I get
file:///C:/Some/some.file
where I wold expect to get the same effect as with Path.Combine
(as that is the old code I need to replace):
file:///C:/Some/Dirs/some.file
I can't think of a clean solution to this.
The ugly solution being to add a /
to the Uri if it's not there
string s = a.OriginalString;
if(s[s.Length-1] != '/')
a = new Uri(s + "/");
Well, you're going to have to tell the Uri somehow that the last part is a directory rather than a file. Using a trailing slash seems to be the most obvious way to me.
Bear in mind that for many Uris, the answer you've got is exactly right. For example, if your web browser is rendering
http://foo.com/bar/index.html
and it sees a relatively link of "other.html" it then goes to
http://foo.com/bar/other.html
not
http://foo.com/bar/index.html/other.html
Using a trailing slash on "directory" Uris is a pretty familiar way of suggesting that relative Uris should just append instead of replacing.
这篇关于如何结合的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!