使用ASP.NET开发服务器测试多个域 [英] Test multiple domains using ASP.NET development server

查看:30
本文介绍了使用ASP.NET开发服务器测试多个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,它将根据用于访问该站点的域名来动态更改其内容.多个域将指向同一应用程序.我希望使用以下代码(或附近的代码)来检测域名并执行自定义:

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;
}

我想使用ASP.NET开发服务器测试以上功能.我在本地HOSTS文件中创建了映射,以将www.clientone.com映射到127.0.0.1,将www.clienttwo.com映射到127.0.0.1.然后,我使用www.clinetone.com(等)使用浏览器浏览到该应用程序.

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).

当我尝试使用ASP.net开发服务器测试此代码时,URL始终显示localhost.它不捕获在浏览器中输入的主机,仅捕获本地主机.

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.

是否可以使用开发服务器测试URL检测功能?

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

谢谢.

推荐答案

我自己解决了这个问题.这里的问题不是HOSTS文件不起作用,而是我使用了错误的方法从浏览器中检测主机标头.

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.

这不起作用,仅重复ASP开发服务器所在的127.0.0.1本地主机.

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

 Request.Url.Host;

但是,通过使用以下内容,可以存储输入浏览器的域,甚至可以在ASP开发服务器上使用该域动态更改站点行为.

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. 在本地计算机上的HOSTS文件中创建指向127.0.0.1的多个测试域
  2. 使用Headers.Get("Host")语法嗅探输入到浏览器中的域

我发现的唯一陷阱"是,您仍然必须手动保留ASP开发服务器正在其上运行的特定端口.

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

示例:如果您的主机文件www.mytestdomain.com中指向127.0.0.1,并且开发服务器在端口46146上运行,则必须在浏览器中输入以下内容进行测试:

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/

但是它仍然有效!

这篇关于使用ASP.NET开发服务器测试多个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆