Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting HttpContext.Current.Request.Url.Host

Tags:

asp.net

I need to replace the host part of the Uri

HttpContext.Current.Request.Url.Host = "newDomain";

you can't set host. Is there a quick and easy way to do this to a Uri for reusing it to redirect somewhere else?

like image 269
PositiveGuy Avatar asked Oct 20 '25 15:10

PositiveGuy


1 Answers

Use the UriBuilder class to change URIs, e.g.

var original = HttpContext.Current.Request.Url;
var changed = (new UriBuilder(original) { Host = "newDomain" }).Uri;

URIs are tricky little beasts with plenty of semantics you might not know or expect, so don't go using string functions on them unless you absolutely have to.

like image 181
Greg Beech Avatar answered Oct 23 '25 04:10

Greg Beech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!