C# HttpContext.Current.Request和Page.Request中的Url.Host
我有以下方法:C# HttpContext.Current.Request和Page.Request中的Url.Host,c#,asp.net,C#,Asp.net,我有以下方法: public static string GetHttpHost(System.Web.HttpRequest hr) { return "http://" + hr.Url.Host + ":" + hr.Url.Port.ToString() ; } 当我使用GetHttpHostthis.Request和GetHttpHostHttpContext.Current.Request调用此方法时,它返回不同的结果 例如: 我的请求页面是
public static string GetHttpHost(System.Web.HttpRequest hr)
{
return "http://" + hr.Url.Host + ":" + hr.Url.Port.ToString() ;
}
当我使用GetHttpHostthis.Request和GetHttpHostHttpContext.Current.Request调用此方法时,它返回不同的结果
例如:
我的请求页面是
在mypage.aspx.cs中,调用GetHttpHostthis.Request返回http://192.168.1.103:80
在呈现mypage.aspx时,会涉及一些业务逻辑,因此会加载BLL.dll。在BLL.dll中,调用GetHttpHostHttpContext.Current.Request返回http://app133:80 app133是我们的web服务器的名称
我在Stack Overflow中搜索,所有相关问题都告诉我HttpContext.Current.Request和Page.Request是同一个对象
那么,谁能告诉我代码中发生了什么
谢谢。否,HttpContext.Current.Request和Page.Request不相同。两者都是同一类HttpRequest的实例,但它们是不同的实例
在每种情况下,私有HttpRequest实例的创建方式都不同——我找不到创建它的确切代码,但请记住HttpContext.Current只创建一次,远远早于任何页面
它可以归结为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;
}
}
如您所见,它首先尝试读取System.Web.HttpWorkerRequest基类中的已知请求头GetKnownRequestHeader方法,并且仅在失败时才会调用GetServerName方法,该方法将返回IP地址或服务器名称,具体取决于Web应用程序的托管位置
没有找到任何官方文档或证据说明为什么一个返回IP而另一个返回机器名,但上面可以解释差异。否,HttpContext.Current.Request和Page.Request不相同。两者都是同一类HttpRequest的实例,但它们是不同的实例
在每种情况下,私有HttpRequest实例的创建方式都不同——我找不到创建它的确切代码,但请记住HttpContext.Current只创建一次,远远早于任何页面
它可以归结为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;
}
}
如您所见,它首先尝试读取System.Web.HttpWorkerRequest基类中的已知请求头GetKnownRequestHeader方法,并且仅在失败时才会调用GetServerName方法,该方法将返回IP地址或服务器名称,具体取决于Web应用程序的托管位置
没有找到任何官方文档或证据说明为什么一个返回IP而另一个返回机器名,但上面的内容可以解释差异。您的问题并不明显,但您使用的是ASP.NET WebForms并从Page类继承?请你把你的问题重新表述清楚,强调上下文。在不知道它是什么的情况下讨论这一点非常困难。this.Request和HttpContext.Current.Request应该相同,但如果您从页面或上下文请求Url,则Url属性的初始化可能不同。如果您总是先请求HttpContext.Current.request.Url,比如“Uri dummyUri=HttpContext.Current.request.Url”,然后使用this.request.Url,会发生什么情况?@ProgrammingHero,我重复我的问题。希望现在一切都清楚了。从您的问题来看这并不明显,但您正在使用ASP.NET WebForms并从Page类继承?请你把你的问题重新表述清楚,强调上下文。在不知道它是什么的情况下讨论这一点非常困难。this.Request和HttpContext.Current.Request应该相同,但如果您从页面或上下文请求Url,则Url属性的初始化可能不同。如果您总是先请求HttpContext.Current.request.Url,比如“Uri dummyUri=HttpContext.Current.request.Url”,然后使用this.request.Url,会发生什么情况?@ProgrammingHero,我重复我的问题。希望现在一切都清楚了。