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