Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path.Combine for URLs?

Path.Combine is handy, but is there a similar function in the .NET framework for URLs?

I'm looking for syntax like this:

Url.Combine("http://MyUrl.com/", "/Images/Image.jpg") 

which would return:

"http://MyUrl.com/Images/Image.jpg"

like image 671
Brian MacKay Avatar asked Dec 16 '08 21:12

Brian MacKay


People also ask

How do I combine paths?

Just switch to the Path Selection tool (Shift-A until it comes up), then go up to the Options Bar and click on the Combine button. Now when you move one path, all the combined paths move right along with it.


2 Answers

Uri has a constructor that should do this for you: new Uri(Uri baseUri, string relativeUri)

Here's an example:

Uri baseUri = new Uri("http://www.contoso.com"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm"); 

Note from editor: Beware, this method does not work as expected. It can cut part of baseUri in some cases. See comments and other answers.

like image 130
Joel Beckham Avatar answered Sep 21 '22 21:09

Joel Beckham


This may be a suitably simple solution:

public static string Combine(string uri1, string uri2) {     uri1 = uri1.TrimEnd('/');     uri2 = uri2.TrimStart('/');     return string.Format("{0}/{1}", uri1, uri2); } 
like image 39
Matthew Sharpe Avatar answered Sep 20 '22 21:09

Matthew Sharpe