Testing multiple domains using ASP.NET Development Server

I am developing one web application that will dynamically change its content depending on the domain name used to access the site. Multiple domains will point to the same application. I want to use the following code (or something close) to determine the domain name and make settings:

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 above using the ASP.NET development server. I created mappings in the local HOSTS file to display www.clientone.com to 127.0.0.1 and www.clienttwo.com to 127.0.0.1. Then I browse the application in 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 fix the host entered in the browser, only local.

Is there a way to test URL discovery functions using a development server?

Thanks.

+4
source share
1 answer

I thought about it myself. The problem here was not that the HOSTS file did not work, but that I used the wrong method to detect the host header from the browser.

This does NOT work and only repeats the local host 127.0.0.1 on which the ASP development server is running.

  Request.Url.Host; 

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

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

So, the solution for checking multiple domains on the Dev server:

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

The only thing I found is that you still have to manually save the specific port on which the ASP dev server is running.

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

But it still works!

+7
source

Source: https://habr.com/ru/post/1305066/


All Articles