简体   繁体   English

HttpContext.Current.Request和Page.Request中的Url.Host

[英]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: 例如:

  • My request page is http://192.168.1.103/mypage.aspx 我的请求页面是http://192.168.1.103/mypage.aspx
  • In mypage.aspx.cs, calling GetHttpHost(this.Request) returns http://192.168.1.103:80 在mypage.aspx.cs中,调用GetHttpHost(this.Request)返回http://192.168.1.103:80
  • When rendering mypage.aspx, some biz logic is involved, so BLL.dll is loaded. 呈现mypage.aspx时,涉及到一些业务逻辑,因此将加载BLL.dll。 In BLL.dll, calling 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.RequestPage.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.

相关问题 HttpContext.Request或HttpContext.Current.Request URL中是否缺少“#”字母? - '#' letter is missing in HttpContext.Request or HttpContext.Current.Request url? 生产中的HttpContext.Current.Request为null - HttpContext.Current.Request null in production 在HttpContext.Current.Request中模拟ServerVariables - Mock ServerVariables in the HttpContext.Current.Request HttpContext.Current.Response/Request 和 Page.Request/Response 之间的区别 - Difference between HttpContext.Current.Response/Request And Page.Request/Response 我可以使用动态重定向HttpContext.Current.Request吗? - can I use dynamic to redirect HttpContext.Current.Request? HttpControllerContext.Request和HttpContext.Current.Request之间的区别 - Difference between HttpControllerContext.Request and HttpContext.Current.Request aspx页面上HttpContext.Current.Request.Url.Host和HttpContext.Current.Request.Url.Authority之间的三元条件 - Ternary condition between HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.Url.Authority on aspx page 带有Angular UI路由器的HttpContext.Current.Request - HttpContext.Current.Request with Angular ui-router 我想在我的Windows窗体中包含HttpContext.Current.Request - I want to include HttpContext.Current.Request in my windows forms 什么HttpContext.Current.Request [“ CarName”]!= null,它在做什么? - what HttpContext.Current.Request[“CarName”] != null, what it is doing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM