Test multiple domains using ASP.NET development server

江枫思渺然 提交于 2019-12-22 08:42:29

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/2513216/test-multiple-domains-using-asp-net-development-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!