HttpRequest.Host.Url 解析为与请求的域名不同的内容 - 可能的罪魁祸首?
我有一个在 Web 主机上运行的 ASP.NET 应用程序。有一些逻辑正在调用
(HttpRequest)Request.Host.Url
动态构造一些 URL 以在锚标记中使用。在我的本地开发环境中,结果符合预期,即 Host.Url 的“localhost”。
然而,在生产服务器上,情况并不像我预期的那样。我正在加载的页面的 URL 如下所示:
http://www.example.com/forms/someform.aspx
使用 HttpRequest.Url.Host 的逻辑正在为该页面构建一些链接,结果实际上是:
http://www.example.com.z82.webhostname.com/forms/somfform.aspx
什么可能导致此情况?我的印象是 HttpRequest.Url.Host 是基于 http 请求标头的,在页面加载的情况下是基本的 www.example.com。
如果 Web 主机使用某种代理服务器来重定向请求(而不是直接将 DNS 重新定向到负责的 ASP.NET 服务器),是否会导致此问题?或者可能是其他什么?
I have an ASP.NET application running on a web host. There is some logic that is calling on
(HttpRequest)Request.Host.Url
to dynamically construct some URLs for use in anchor tags. On my local dev environemnt, the result is as expected, namely "localhost" for Host.Url.
However, on the production server, it is not as I would expect. The URL of the page I am loading looks like:
http://www.example.com/forms/someform.aspx
The logic that uses HttpRequest.Url.Host is building some links for the page, and the result is actually:
http://www.example.com.z82.webhostname.com/forms/somfform.aspx
What could cause this? I was under the impression that HttpRequest.Url.Host was based on the http request headers, which in the case of the page load is the basic www.example.com.
If the webhost used some sort of proxy server to re-direct the request (as opposed to the DNS resloving directly to the responsible ASP.NET server), would that result in this problem? Or could it be something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我观察到类似的行为,但我不太明白为什么 .NET/IIS 以如此奇怪的方式处理请求。另一个有趣的测试是使用
Request.Url.ToString()
它将报告一个地址,然后使用string.Replace()
(或其他字符串方法)它将报告一个地址不同的地址。似乎与DNS记录(一般环境设置)有关。
I have observed similar behavior but I don't understand well why .NET/IIS is handling the request in such a strange way. Another interesting test is to use
Request.Url.ToString()
which will report one address and then usestring.Replace()
(or another string method) which will report a different address.It seems to be related to DNS records (environment settings in general).
我相信
Request.Url.Host
所描绘的是您的客户端的 TCP 堆栈碰巧遇到的任何反向 DNS。如果我ping www.google.com
,也会发生同样的事情;我Ping www.l.google.com [74.125.227.51],数据为 32 字节...
。不是同一个主机。如果您想要在浏览器地址栏中输入的确切主机名,请使用 HTTP 主机标头:
Request.Headers["Host"]
请注意
Request.Headers["Host"]
将包含端口号(如果地址栏中存在),因此如果您只需要主机名,请使用Request.Headers["Host"].Split(':')[0]< /代码>。
I believe what
Request.Url.Host
is portraying is whatever reverse DNS the TCP stack of your client happened to come across. The same thing happens if Iping www.google.com
; I getPinging www.l.google.com [74.125.227.51] with 32 bytes of data...
. Not the same host.If you want the exact host name that was typed into the browser's address bar, use the HTTP Host header:
Request.Headers["Host"]
Note that
Request.Headers["Host"]
will include a port number if present in the address bar, so if you're expecting the host name only, useRequest.Headers["Host"].Split(':')[0]
.