Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How to reliably get the real URL of the current request?

Right now I have an MVC application running on http://127.0.0.1:8081/ (it is actually running in the Azure compute emulator). The browser location bar clearly says 8081 for the port number.

However, Request.Url and Request.RawUrl both give me http://127.0.0.1:8082/.

I need to send an e-mail with a URL in it, so I need the correct hostname and port number. How do I get the actual, real URL reliably and without such unexpected deviation?

like image 631
Timwi Avatar asked Dec 14 '25 15:12

Timwi


1 Answers

You can use Request.Headers["Host"]; which will give you the host name (or IP) with the correct port number. Then you can reconstruct the URL.

like image 145
Teoman Soygul Avatar answered Dec 16 '25 06:12

Teoman Soygul



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!