【问题标题】:Getting the domain's root获取域的根
【发布时间】:2016-01-18 18:37:48
【问题描述】:

当我尝试获取http://subdomain.domain.com 域的主机时,结果总是“127.0.0.1”。我尝试了几种变体,例如

rootUrl = string.Format("{0}://{1}{2}{3}",
                        context.Request.Url.Scheme,
                        context.Request.Url.Host,
                        context.Request.Url.Port == 80 ? ""
                                                       : ":" + context.Request.Url.Port,
                        context.Request.ApplicationPath);

我还尝试了this SO page 的其他建议,但结果仍然是 IsLocal=true、Port=80 和 Host="127.0.0.1"。为什么我在实时网页上没有得到 Host 或 Request.Url.Authority 的“subdomain.domain.com”?

编辑 当从不同的、完全不相关的机器访问该站点时,也会发生同样的事情。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您的代码没有问题,看起来该网站也可以访问
    通过visual studio(IIS express)或客户端(浏览器)以及Web应用程序在同一系统中运行。

    【讨论】:

    • 网站是通过浏览器访问的,而不是通过VS;我的机器上没有同时运行的竞争应用程序
    • 您是如何访问您的网站的? subdomain.domain.com 还是 localhost/VirtualDirectoryName?如果是 localhost,则该行为是预期的。
    • 它只是“subdomain.domain.com”,在任何地方都没有本地主机;正如上面的编辑中提到的,从不同的机器访问网站也会发生同样的事情
    【解决方案2】:

    这对我有用:

    在 ASP.NET 中:

                var ServerBaseUrl = Request.Url.Scheme + "://"
                      + Request.Url.Authority 
                      + (Request.Url.IsDefaultPort ? string.Empty : ":" + Request.Url.Port)
                      + Request.ApplicationPath.TrimEnd('/');
    

    在 WebAPI 中:

                var ServerBaseURL = Request.RequestUri.Scheme + "://"
                                + Request.RequestUri.Authority
                                + (Request.RequestUri.IsDefaultPort ? string.Empty : ":" + Request.RequestUri.Port)
                                + (string.IsNullOrWhiteSpace(Request.GetRequestContext().VirtualPathRoot.Trim('/'))
                                    ? string.Empty
                                    : (Request.GetRequestContext().VirtualPathRoot));
    

    【讨论】:

    • 谢谢,但 Request.RequestUri.Authority 也返回 "127.0.0.1" :(
    • 您是否在此站点的 IIS 设置中绑定到“*”(所有域)并通过运行 IIS 的服务器访问它?你在浏览器中输入了什么网址?
    • 没有特定的绑定,只是从我的本地机器访问某个 subdomain.domain.com,由主要提供商托管;我希望返回浏览器中显示的网页的根目录
    猜你喜欢
    • 2010-12-04
    • 2012-05-12
    • 2023-03-27
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    相关资源
    最近更新 更多