2010-11-18 95 views

回答

2

那麼,你需要確保你的基礎URI有/字符結尾:

public Uri CombineUris(string baseUri, string relativeUri) 
{ 
    if (!baseUri.EndsWith("/")) { 
     baseUri += "/"; 
    } 
    return new Uri(new Uri(baseUri), relativeUri); 
} 
1

確保通過根URI與後/。最後的斜線非常重要。考慮http://www.example.com/foo/bar.html, bar2.html。它應該被解析爲http://www.example.com/foo/bar2.html

0
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 + "/"; 
} 
+0

它看起來像你的'URI = null'條件反轉。 – 2010-11-18 09:00:37

+0

null!= uri曾經是C/C++世界的一個良好實踐。但是,在C#中,如果總是期望一個布爾值,因此不會意外分配。 – Ramesh 2010-11-18 09:04:58

+0

不,我的意思是如果它不是''null'就返回'uri'。我認爲你想要返回它,如果它*是*'null' :) – 2010-11-18 09:36:45