13

I need to server parts of my application from different domains. To be precise I have a sub section of the site that should be served from a region specific domain. For example:

  • /fr/* should be served from www.domain.fr
  • /uk/* should be serverd from www.domain.co.uk and so on.

I'd like to make a route entry that will redirect the request with wrong domain to the correct domain. But I don't know how to access http header information form HttpContext.

Any help is welcome.

Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122

4 Answers4

23
string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
string requestScheme = HttpContext.Current.Request.Url.Scheme;
string requestQueryString = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
string requestUrl = HttpContext.Current.Request.ServerVariables["URL"];
Aaron
  • 802
  • 7
  • 17
7

HttpContext.Current.Request.Url.Host

Brunno
  • 977
  • 17
  • 28
eugeneK
  • 10,750
  • 19
  • 66
  • 101
  • HttpContext.Request.ServerVariables["HTTP_HOST"]; -- done the trick – Piotr Czapla Oct 29 '09 at 15:21
  • 2
    It doesn't work, It always returns the same value. (ie. It returns localhost even if I access the site using 127.0.0.1 or "Anyotherdomain or ip address") – Piotr Czapla Oct 29 '09 at 15:22
  • 1
    `Reqiest.Url.Host` will only have the Host header value if the app setting "aspnet:UseHostHeaderForRequestUrl" has the value of "true" (the default is "false"). – Richard Szalay May 16 '19 at 06:26
5

I think you want Request.Headers["host"]

Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
0

In some cases you want to know from the host who sent de request, so I used this

 stsring _Host = HttpContext.Request.Headers["Referer"];
Ruben
  • 139
  • 1
  • 3