I Have a Path.Combine, But How About a Url.Combine?
October 30th, 2008
If you’re wanting to combine two URLs with Path.Combine, then you’re likely to wind up with something like “http://bitterware.com\download.html”… and that’s not very good. What we need is something like a Url.Combine. Well, we have something like that. We have Uri.TryCreate, but it’s not as pretty as Path.Combine. So I prettied it up, wrapped it up and thought I’d share:
namespace Bitter
{
namespace Path
{
public static class Url
{
public static string Combine(string domain, string page)
{
string combinedUrl = String.Empty;
Uri baseUri = null;// create the URI object from the string domain
try
{
baseUri = new Uri(domain);
}
catch (Exception ex)
{
baseUri = null;
}// try to combine and create the new URI
if (baseUri != null)
{
Uri value = null;
if (Uri.TryCreate(baseUri, page, out value))
combinedUrl = value.ToString();
}return combinedUrl;
}…




