[英]Url.Host in HttpContext.Current.Request and Page.Request
I have following method: 我有以下方法:
public static string GetHttpHost(System.Web.HttpRequest hr)
{
return "http://" + hr.Url.Host + ":" + hr.Url.Port.ToString() ;
}
When I call this method with GetHttpHost(this.Request)
and GetHttpHost(HttpContext.Current.Request)
, it returns different results. 当我使用GetHttpHost(this.Request)
和GetHttpHost(HttpContext.Current.Request)
调用此方法时,它将返回不同的结果。
For example: 例如:
GetHttpHost(this.Request)
returns http://192.168.1.103:80
在mypage.aspx.cs中,调用GetHttpHost(this.Request)
返回http://192.168.1.103:80
GetHttpHost(HttpContext.Current.Request)
returns http://app133:80
(app133 is our web server's name) 在BLL.dll中,调用GetHttpHost(HttpContext.Current.Request)
返回http://app133:80
(app133是我们的Web服务器的名称) I searched in Stack Overflow, all related questions tell me that HttpContext.Current.Request and Page.Request are same object. 我在Stack Overflow中进行搜索,所有相关问题都告诉我HttpContext.Current.Request和Page.Request是同一对象。
So, can anyone tell me what happened in my code? 那么,谁能告诉我代码中发生了什么?
Thanks. 谢谢。
No, HttpContext.Current.Request
and Page.Request
are not the same. 不, HttpContext.Current.Request
和Page.Request
是不同的。 Both are instances of the same class (HttpRequest) but those are different instances. 两者都是同一类(HttpRequest)的实例,但它们是不同的实例。
In each case, the private HttpRequest
instance is created differently - I could not find the exact code creating it but keep in mind that HttpContext.Current
is created only once, long before any Page. 在每种情况下,私有HttpRequest
实例的创建方式都是不同的-我找不到创建该实例的确切代码,但请记住, HttpContext.Current
仅创建一次,并且早于任何Page。
It all boils down to the following code in HttpRequest class: 一切都归结为HttpRequest类中的以下代码:
public Uri Url
{
get
{
if (this._url == null && this._wr != null)
{
string text = this.QueryStringText;
if (!string.IsNullOrEmpty(text))
{
text = "?" + HttpUtility.CollapsePercentUFromStringInternal(text, this.QueryStringEncoding);
}
if (AppSettings.UseHostHeaderForRequestUrl)
{
string knownRequestHeader = this._wr.GetKnownRequestHeader(28);
try
{
if (!string.IsNullOrEmpty(knownRequestHeader))
{
this._url = new Uri(string.Concat(new string[]
{
this._wr.GetProtocol(),
"://",
knownRequestHeader,
this.Path,
text
}));
}
}
catch (UriFormatException)
{
}
}
if (this._url == null)
{
string text2 = this._wr.GetServerName();
if (text2.IndexOf(':') >= 0 && text2[0] != '[')
{
text2 = "[" + text2 + "]";
}
this._url = new Uri(string.Concat(new string[]
{
this._wr.GetProtocol(),
"://",
text2,
":",
this._wr.GetLocalPortAsString(),
this.Path,
text
}));
}
}
return this._url;
}
}
As you can see, it first tries to read known request header ( GetKnownRequestHeader
method in System.Web.HttpWorkerRequest
base class) and only if it fails it will invoke GetServerName
method which will return either IP address or server name depending where the web application is hosted. 如您所见,它首先尝试读取已知的请求标头( System.Web.HttpWorkerRequest
基类中的GetKnownRequestHeader
方法),只有失败时,它才会调用GetServerName
方法,该方法将返回IP地址或服务器名称,具体取决于Web应用程序所在的位置托管。
Didn't find any official documentation or proof as to why exactly one returns IP and other the machine name, but the above can explain the difference. 找不到任何正式文档或证明为什么一个人确实返回IP而其他人返回机器名的证明,但是以上内容可以解释其中的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.