【问题标题】:How to get "Host:" header from HttpContext (asp.net)如何从 HttpContext (asp.net) 获取“主机:”标头
【发布时间】:2009-10-28 17:08:25
【问题描述】:

我需要为来自不同域的应用程序的某些部分提供服务器。 准确地说,我有一个站点的子部分,应该从特定区域的域提供服务。例如:

  • /fr/* 应从 www.domain.fr 提供
  • /uk/* 应从 www.domain.co.uk 提供服务 等等。

我想创建一个路由条目,将错误域的请求重定向到正确的域。但是我不知道如何从HttpContext获取http头信息。

欢迎任何帮助。

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:
    string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
    string requestScheme = HttpContext.Current.Request.Url.Scheme;
    string requestQueryString = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
    string requestUrl = HttpContext.Current.Request.ServerVariables["URL"];
    

    【讨论】:

    • HttpContext.Request.ServerVariables["HTTP_HOST"] 是我想要的。谢谢
    【解决方案2】:

    HttpContext.Current.Request.Url.Host

    【讨论】:

    • HttpContext.Request.ServerVariables["HTTP_HOST"]; -- 成功了
    • 它不起作用,它总是返回相同的值。 (即,即使我使用 127.0.0.1 或“任何其他域或 IP 地址”访问该站点,它也会返回 localhost)
    • Reqiest.Url.Host 仅在应用设置“aspnet:UseHostHeaderForRequestUrl”的值为“true”(默认为“false”)时才具有 Host 标头值。
    【解决方案3】:

    我想你想要 Request.Headers["host"]

    【讨论】:

      【解决方案4】:

      在某些情况下你想从发送de请求的主机那里知道,所以我用了这个

       stsring _Host = HttpContext.Request.Headers["Referer"];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 2010-12-31
        • 2018-10-25
        • 2020-05-08
        • 1970-01-01
        • 2021-08-13
        • 1970-01-01
        相关资源
        最近更新 更多