Uri test = new Uri(new Uri(“http://www.google.com/test”),“foo”);使用Uri的相对路径
但乌里测试=新URI(新URI( “http://www.google.com/test/”), “foo” 的);
回报http://www.google.com/foo/test
看来最后一个斜线是非常重要的,有没有在所有情况下返回 http://www.google.com/foo/test一种统一的方式
Uri test = new Uri(new Uri(“http://www.google.com/test”),“foo”);使用Uri的相对路径
但乌里测试=新URI(新URI( “http://www.google.com/test/”), “foo” 的);
回报http://www.google.com/foo/test
看来最后一个斜线是非常重要的,有没有在所有情况下返回 http://www.google.com/foo/test一种统一的方式
那么,你需要确保你的基础URI有/
字符结尾:
public Uri CombineUris(string baseUri, string relativeUri)
{
if (!baseUri.EndsWith("/")) {
baseUri += "/";
}
return new Uri(new Uri(baseUri), relativeUri);
}
确保通过根URI与后/
。最后的斜线非常重要。考虑http://www.example.com/foo/bar.html, bar2.html
。它应该被解析为http://www.example.com/foo/bar2.html
。
Uri test = new Uri(new Uri(GetSafeURIString("http://www.google.com/test")), "foo");
private static string GetSafeURIString(uri)
{
if(uri == null)
return uri;
else
return uri.EndsWith("/") ? uri : uri + "/";
}
它看起来像你的'URI = null'条件反转。 – 2010-11-18 09:00:37
null!= uri曾经是C/C++世界的一个良好实践。但是,在C#中,如果总是期望一个布尔值,因此不会意外分配。 – Ramesh 2010-11-18 09:04:58
不,我的意思是如果它不是''null'就返回'uri'。我认为你想要返回它,如果它*是*'null' :) – 2010-11-18 09:36:45