Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET URI: How can I change ONE part of a URI?

Often I want to change just one part of a URI and get a new URI object back.

In my current dilemma, I want to append .nyud.net, to use the CoralCDN.

I have a fully qualified URI fullUri. How can I, in effect, do this:

fullUri.Host = fullUri.Host + ".nyud.net"; 

This needs to work for almost any URL, and the PORT of the request needs to be maintained.

Any help would be much appreciated.

like image 819
John Gietzen Avatar asked Jan 29 '10 16:01

John Gietzen


1 Answers

You can use an UriBuilder to modify individual parts of an Uri:

Uri uri = new Uri("http://stackoverflow.com/questions/2163191/");  UriBuilder builder = new UriBuilder(uri); builder.Host += ".nyud.net";  Uri result = builder.Uri; // result is "http://stackoverflow.com.nyud.net/questions/2163191/" 
like image 192
dtb Avatar answered Oct 05 '22 22:10

dtb