windows 如何在开发机器上测试子域?本地主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/872868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test subdomains on a development machine? abc.localhost
提问by Josh
I am trying to develop an asp.net site with multiple subdomains. I am new to web sites in general, and asp.net in particular. However, it seems that wildcard subdomains are properly setup with a combination of dns entries and web server configuration - which isn't going to happen on my development machine. Therefore I'm manually inserting entries in my windows hosts file:
我正在尝试开发一个具有多个子域的 asp.net 站点。我是一般网站的新手,尤其是 asp.net。但是,通配符子域似乎是通过 dns 条目和 Web 服务器配置的组合正确设置的 - 这不会在我的开发机器上发生。因此,我在 Windows主机文件中手动插入条目:
127.0.0.1 localhost
127.0.0.1 abc.localhost
127.0.0.1 xyz.localhost
However, when I try to interrogate the Request.Urlproperty there is no subdomain to be seen. For example, if I hit http://abc.localhost:1660/
in the browser I get http://localhost:1660/
from Request.Uri.ToString()
; the abc
is just gone?!
但是,当我尝试询问Request.Url属性时,看不到子域。例如,如果我http://abc.localhost:1660/
在浏览器中点击,我会http://localhost:1660/
从Request.Uri.ToString()
; 在abc
刚刚走了?
I don't know why the hosts file works like this, but is there any other method I can use to get subdomains into my local web application? Thank you all.
我不知道为什么 hosts 文件是这样工作的,但是有没有其他方法可以用来将子域放入我的本地 Web 应用程序中?谢谢你们。
Note that I'm only using the built-in asp.net development server rather than a full iis server. (I can't get access to a full IIS this weekend, but I would still like to know if that would help.)
请注意,我仅使用内置的 asp.net 开发服务器,而不是完整的 iis 服务器。(本周末我无法访问完整的 IIS,但我仍然想知道这是否有帮助。)
采纳答案by Chris Pietschmann
You can get the requested domain with Subdomain intact by using "Request.Headers["HOST"]". Here's a simple method that returns the Subdomain of the current request. This method also assumes that you have a ".COM", ".NET", etc. after the domain just like the real web. So you'll want to change your HOSTS file to include "localhost.com", "abc.localhost.com", etc.
您可以使用“ Request.Headers["HOST"]”获取完整的子域请求的域。这是一个返回当前请求的子域的简单方法。此方法还假设您在域后有“.COM”、“.NET”等,就像真实的网络一样。因此,您需要更改 HOSTS 文件以包含“localhost.com”、“abc.localhost.com”等。
public string subdomain()
{
string host = Request.Headers["HOST"];
if (!string.IsNullOrEmpty(host))
{
var parts = host.Split('.');
if (parts.Length > 2)
{
return parts[0];
}
}
return string.Empty;
}
I was searching on this very thing and here's an article that is what actually helped me figure this out: https://web.archive.org/web/20090813174916/http://blogs.securancy.com/post/ASPNET-MVC-Subdomain-Routing.aspx
我正在搜索这件事,这里有一篇文章实际上帮助我解决了这个问题:https: //web.archive.org/web/20090813174916/http: //blogs.securancy.com/post/ASPNET-MVC -子域-Routing.aspx
回答by Joe White
Don't know about the headers, but one little-known trick that I've used is that all of the 127.* addresses are localhost addresses, not just 127.0.0.1. You can actually run one server listening on 127.0.0.1 port 80, and another webserver instance listening on 127.0.0.2 port 80. So then you'd name 127.0.0.1 localhost, 127.0.0.2 could be abc.localhost (or abc.mydomain.com so you can test the real live URLs against your local webserver), etc.
不知道标头,但我使用的一个鲜为人知的技巧是所有 127.* 地址都是本地主机地址,而不仅仅是 127.0.0.1。您实际上可以运行一个服务器在 127.0.0.1 端口 80 上侦听,另一个网络服务器实例在 127.0.0.2 端口 80 上侦听。因此,您将命名 127.0.0.1 localhost,127.0.0.2 可以是 abc.localhost(或 abc.mydomain .com 以便您可以针对本地网络服务器测试真实的实时 URL)等。
回答by Vaibhav Garg
I am using Windows 7, IIS 7.5, VS 2008, SQL server 2005
我使用的是 Windows 7、IIS 7.5、VS 2008、SQL server 2005
I was able to successfully simulate sub-domain on localhost buy putting the following line in etc/hosts
我能够在 localhost 上成功模拟子域购买将以下行放入 etc/hosts
127.0.0.2 myapp.localhost.com
and, in IIS I set up a new web site with the following modifications to the bindings section:
并且,在 IIS 中,我建立了一个新网站,对绑定部分进行了以下修改:
Hostname = myapp.localhost.com
IP address = 127.0.0.2
I also created a new application pool but it is not necessary. You may have to change the auth user that is used to work on the requests. I had SQL connectivity permission errors with the default IIS user.
我还创建了一个新的应用程序池,但这不是必需的。您可能需要更改用于处理请求的 auth 用户。默认 IIS 用户出现 SQL 连接权限错误。
IIS>Application Pools>>Right Click on your pool>Advanced Settings>Identity = LocalSystem