Host file hostname is not returned in C # ASP.NET MVC

I am currently testing a site with multiple subdomains pointing to the same ASP.NET application, and routing processes what needs to be done with each request.

For testing, I added several subdomains to my "hosts file", for example. "127.0.0.1 admin.TestDomain.com", which works fine.

However, the problem is that when I call any function in C # to get the hostname / domain / url (HttpContext.Current.Request.Url ...), the host url always returns with "localhost", and than " TestDomain ".

Any ideas why this name is resolved this way, and where can I get "TestDomain.com"?

+8
asp.net-mvc dns hosts
source share
1 answer

I think the original host is lost after matching the domain with the IP address (localhost) of the local operating system with your "host" file. You can try RawUrl instead of getting the exact URL entered in the browser:

HttpContext.Current.Request.RawUrl 

You can also try to extract the HTTP_HOST variable from the Host: user request header, it should contain the source host (not the server address or the server’s default server), which is trying to request:

 string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; 

Perhaps Getting parameters from a RawUrl article would be helpful.

+11
source share

All Articles