I am currently testing a site with multiple sub domains pointing to the same ASP.NET application, and the routing handles what to do with each request.
For testing, I have added several sub domains to my "hosts file", e.g. "127.0.0.1 admin.TestDomain.com", which is working fine.
However, the problem is that when I call any function in c# to get the host name/domain/url (HttpContext.Current.Request.Url...), the host url always comes back with "localhost", rather than "TestDomain".
Any ideas why this name is being resolved in this manner, and where I can get hold of "TestDomain.com"?
I think, original host is lost after mapping of domain to IP-adress (localhost) by local operating system with your "host" file. You can try RawUrl instead to retrieve exact URL typed in browser:
HttpContext.Current.Request.RawUrl
Also you can try to fetch HTTP_HOST
variable from user Host:
request header, it should contain original host (not address or default host of the server) browser tries to request:
string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
Maybe Getting parameters from RawUrl article will be helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With