February 03, 2014
The file system have the Path.Combine method to combine paths but how to combine a base URL with an absolute or relative URL/URI?The answer is to use the System.Uri-constructor to combine the URL:
public static Uri CombineUri(string baseUri, string relativeOrAbsoluteUri) {
return new Uri(new Uri(baseUri), relativeOrAbsoluteUri);
}
public static string CombineUriToString(string baseUri, string relativeOrAbsoluteUri) {
return new Uri(new Uri(baseUri), relativeOrAbsoluteUri).ToString();
}
..
// Results in "http://www.my.domain/relative/path"
var a = CombineUriToString("http://www.my.domain/", "relative/path");
// Results in "http://www.my.domain/absolute/path"
var b = CombineUriToString("http://www.my.domain/something/other", "/absolute/path");
Comments
comments powered by Disqus