I have an ASP.NET application running on a web host. There is some logic that is calling on
to dynamically construct some URLs for use in anchor tags. On my local dev environemnt, the result is as expected, namely "localhost" for Host.Url.
However, on the production server, it is not as I would expect. The URL of the page I am loading looks like:
The logic that uses HttpRequest.Url.Host is building some links for the page, and the result is actually:
What could cause this? I was under the impression that HttpRequest.Url.Host was based on the http request headers, which in the case of the page load is the basic www.example.com.
If the webhost used some sort of proxy server to re-direct the request (as opposed to the DNS resloving directly to the responsible ASP.NET server), would that result in this problem? Or could it be something else?
I have observed similar behavior but I don't understand well why .NET/IIS is handling the request in such a strange way. Another interesting test is to use
Request.Url.ToString() which will report one address and then use
string.Replace() (or another string method) which will report a different address.
It seems to be related to DNS records (environment settings in general).
I believe what
Request.Url.Host is portraying is whatever reverse DNS the TCP stack of your client happened to come across. The same thing happens if I
ping www.google.com; I get
Pinging www.l.google.com [18.104.22.168] with 32 bytes of data.... Not the same host.
If you want the exact host name that was typed into the browser's address bar, use the HTTP Host header:
Request.Headers["Host"] will include a port number if present in the address bar, so if you're expecting the host name only, use