8

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

Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59
  • How are you trying to "get the host name/domain/url"? – Maxim V. Pavlov Mar 29 '12 at 23:16
  • Tried many ways, but they all involve the HttpContext.Current.Request.Url class, and i can't find the "TestDomain" anywhere when inspecting that class on a debug break. – Paul Grimshaw Mar 29 '12 at 23:18
  • Could you please be more specific; show us a controller action and *exactly* the code, and *exactly* what it outputs? Also, what are you running under when you get the results you mention? And what browser(s)? I ask because I've not seen this behavior myself, so I'd like to try to duplicate it. – Andrew Barber Apr 26 '12 at 13:39

1 Answers1

11

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.

Serge S.
  • 4,855
  • 3
  • 42
  • 46
  • I had considered this myself, but from the question I think he's using the URL Routing that is used by default in ASP.NET MVC. Routing actually does not do rewriting (as used by default), and I don't think I've seen similar behavior myself; I always see exactly what's in the address bar as the `Host`. I think! But this is definitely something to look more closely into... – Andrew Barber Apr 26 '12 at 15:03
  • An example of the difference between Routing and Rewriting: When using Rewriting, the IIS Logs actually show the *rewritten* URL, which explains why the information in this answer is correct about rewriting. Routing, however, results in the same URLs showing in the IIS logs as was requested. – Andrew Barber Apr 26 '12 at 15:05
  • I'm sure that problem appeared before MVC Routing. Yes you're right, regarding re-writing - there is actually *mapping* of domain to IP=localhost performed by local operating system (is corrected, Thank you for notes). Browser requesting host TestDomain.com accessed 127.0.0.1, then the server somehow believes that it is a localhost. Anyway `Host:` header of the HTTP request should be original TestDomain.com – Serge S. Apr 26 '12 at 19:17
  • 3
    Perfect. Raw URL gave me just the stem (e.g. "/home") but the ServerVariables["HTTP_HOST"] sorted it for me. Many thanks! I'm not going to dig too much deeper into this one, but happy to provide information / carry out further tests if there is interest. – Paul Grimshaw Apr 26 '12 at 22:08