Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosts file hostname not being returned in c# ASP.NET MVC

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"?

like image 525
Paul Grimshaw Avatar asked Mar 29 '12 23:03

Paul Grimshaw


1 Answers

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.

like image 160
Serge S. Avatar answered Nov 01 '22 15:11

Serge S.