Search code examples
asp.netasp.net-routing

Changing website according to subdomain


I have a website written in ASP.NET. I would like to add subdomains of states. such as nevada.mysite.com. However, I'd like my whole site to be custom made for that subdomain. That is, I'd like to capture in each page which subdomain context I am and show different things.

I do not want to seperate to different websites. I want them all to reside in the same website in the IIS

what is the best and proper way of handling such issue? where do you suggest to hold and save the global variable of the state?

Thanks!


Solution

  • First, have all of your subdomains point to a single IP address in DNS.

    Next, configure IIS to listen for all connections on that IP (don't specify a host name)

    Then write an HttpModule (or perhaps use Global.asax) with a handler for the BeginRequest event. Extract the subdomain name from the incoming URL, and store it in HttpContext.Items["state"] (the Items Dictionary is unique per request).