为什么请求["主机"] ==" dev.testhost.com:1234"而Request.Url.Host =="本地主机" [英] Why does Request["host"] == "dev.testhost.com:1234" whereas Request.Url.Host == "localhost"
问题描述
大家好,我似乎与Visual Studio 2008(卡西尼)内置的Web服务器上的本地测试ASP.NET应用程序时发现的差异。
我已经设置了我的本地机器的主机相关联的 dev.testhost.com 是 127.0.0.1 ,因为我有一个需要改变其外观的应用根据所使用的主机头调用它。
I've set up a host on my local machine associating dev.testhost.com with 127.0.0.1, since I have an application that needs to change its appearance depending on the host header used to call it.
然而,当我要求使用 http://dev.testhost.com:1234/index.aspx
我的测试应用程序,的价值Request.Url.Host
总是本地主机
。鉴于对 Request.Headers值[主机]
是dev.testhost.com:1234
(如我希望他们的两个的是)。
However, when I request my test application using http://dev.testhost.com:1234/index.aspx
, the value of Request.Url.Host
is always "localhost"
. Whereas the value of Request.Headers["host"]
is "dev.testhost.com:1234"
(as I would expect them both to be).
我不担心,第二个值包含端口号,但是我强大的困惑,为什么在主机名是完全不同的!有谁知道这是一个已知的问题,还是设计?还是我是一个傻瓜?!
I'm NOT concerned that the second value includes the port number, but I am mighty confused as to why the HOST NAMES are completely different! Does anyone know if this is a known issue, or by design? Or am I being an idiot?!
<击>我宁愿使用 Request.Url.Host
,因为这避免了测试... 击>时剥离出的端口号 - 的删除,原因是可能会造成混乱! - 山姆的
I'd rather use - Removed due to possibly causing confusion! - SamRequest.Url.Host
, since that avoids having to strip out the port number when testing...
推荐答案
Request.Headers [主机]
是连接到服务器的应用程序接收的价值,而其他一个值是当它试图获得该域名的服务器获取。
Request.Headers["host"]
is the value received from the application that connects to the server, while the other value is the one the server gets when it tries to get the domain name.
浏览器中的域名进入,因为在虚拟域的情况下使用的请求使用。服务器报告服务器preferences一组,或者找到的第一个。
The browser uses in the request the domain name entered because that is used in the case of virtual domains. The server reports the one set in the server preferences, or the first one it finds.
编辑:纵观卡西尼号的code,看它是否使用了一些特殊的设置,我注意到以下code:
Looking at the code of Cassini to see if it uses some particular settings, I noticed the following code:
public string RootUrl {
get {
if (_port != 80) {
return "http://localhost:" + _port + _virtualPath;
}
else {
return "http://localhost" + _virtualPath;
}
}
}
//
// Socket listening
//
public void Start() {
try {
_socket = CreateSocketBindAndListen(AddressFamily.InterNetwork, IPAddress.Loopback, _port);
}
catch {
_socket = CreateSocketBindAndListen(AddressFamily.InterNetworkV6, IPAddress.IPv6Loopback, _port);
}
// …
}
的解释似乎是,卡西尼明确提到为localhost,并且不会尝试做一个反向DNS查找。不同的是,它不会使用回报的http:// localhost的。+ _virtualPath;
这篇关于为什么请求[&QUOT;主机&QUOT;] ==&QUOT; dev.testhost.com:1234&QUOT;而Request.Url.Host ==&QUOT;本地主机&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!