Test multiple domains using ASP.NET development server

I am developing a single web application that will dynamically change its content depending on which domain name is used to reach the site. Multiple domains will point to the same application. I wish to use the following code (or something close) to detect the domain name and perform the customizations:

string theDomainName = Request.Url.Host;

switch (theDomainName)
{
  case "www.clientone.com":
    // do stuff
    break;
  case "www.clienttwo.com":
    // do other stuff
    break;
}

I would like to test the functionality of the above using the ASP.NET development server. I created mappings in the local HOSTS file to map www.clientone.com to 127.0.0.1, and www.clienttwo.com to 127.0.0.1. I then browse to the application with the browser using www.clinetone.com (etc).

When I try to test this code using the ASP.net development server the URL always says localhost. It does NOT capture the host entered in the browser, only localhost.

Is there a way to test the URL detection functionality using the development server?

Thanks.

Answers


Figured this one out on my own. The problem here wasn't that the HOSTS file didn't work, it was that I was using the wrong method to detect the host header from the browser.

This does NOT work, and only repeats the 127.0.0.1 localhost that the ASP development server lives on.

 Request.Url.Host;

However, by using the following instead, the domain entered into the browser is stored and can be used to dynamically change the site behavior, even on the ASP development server.

 HttpContext.Current.Request.Headers.Get("Host").ToString();  

So the solution to test multiple domains on a Dev server is:

  1. create several test domains in the HOSTS file on your local machine pointing to 127.0.0.1
  2. use the Headers.Get("Host") syntax to sniff the domain entered into the browser

The only 'gotcha' that I found is that you must still manually preserve the specific port that the ASP dev server is running on.

Example: if you have in your hosts file www.mytestdomain.com pointing to 127.0.0.1, and your dev server is running on port 46146, then you must enter the following into your browser for testing: http://www.mytestdomain.com:46146/

But it still works!


Need Your Help

blackberry webworks: trackpad scrolling

javascript html blackberry blackberry-webworks

i have several focusable elements in my app, so when i'm focusing a scrollable div, the focus goes to another focusable element instead of scrolling that div when using the trackpad. how can i prev...

yii modal dialog login

login yii modal-dialog

I've been looking all over the internet and I can't seem to find the solution to just a simple login via header. I tried all sorts of way. The problem I have right now just reloads every time i hit...