【问题标题】:How can I get the root domain URI in ASP.NET?如何在 ASP.NET 中获取根域 URI?
【发布时间】:2010-11-15 22:08:24
【问题描述】:

假设我在http://www.foobar.com 托管一个网站。

有没有一种方法可以在我的代码中以编程方式确定“http://www.foobar.com/”(即无需在我的网络配置中对其进行硬编码)?

【问题讨论】:

  • 由于这取决于请求,您可以尝试查看Request 对象。

标签: asp.net url


【解决方案1】:

HttpContext.Current.Request.Url 可以为您获取 URL 上的所有信息。并且可以将url分解成片段。

【讨论】:

  • 是的,为什么投反对票?不要看到标记为答案的东西 - 并且 - 经常被否决。 ://
  • 我也不喜欢这个答案。 blesh 给出了正确的答案,这应该被标记为答案......
  • -1 - Request.Url 经常给出一个诸如“/folder1/folder2”之类的url,并完全排除域。
  • @Justin:Request.Url 为您提供了一个 Uri 对象,它为您分解了所有部分。它不应该给你一个字符串。至少不在我使用的 .net 版本中
  • 这个答案可以通过添加代码来改进,使其像下面有更多投票的答案一样工作......
【解决方案2】:

获取整个请求 URL 字符串:

HttpContext.Current.Request.Url

获取请求的 www.foo.com 部分:

HttpContext.Current.Request.Url.Host

请注意,在某种程度上,您会受到 ASP.NET 应用程序之外的因素的影响。如果 IIS 配置为您的应用程序接受多个或任何主机标头,则通过 DNS 解析到您的应用程序的任何域都可能显示为请求 URL,具体取决于用户输入的 URL。

【讨论】:

  • 这里最简单的解决方案
【解决方案3】:
string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"

【讨论】:

    【解决方案4】:

    下面的 C# 示例:

    string scheme = "http://";
    string rootUrl = default(string);
    if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "on")
    {
      scheme = "https://";
    }
    rootUrl = scheme + Request.ServerVariables["SERVER_NAME"].ToString();
    

    【讨论】:

      【解决方案5】:
      string domainName = Request.Url.Host
      

      【讨论】:

        【解决方案6】:

        对于仍有疑问的任何人,http://devio.wordpress.com/2009/10/19/get-absolut-url-of-asp-net-application/ 提供更完整的答案。

        public string FullyQualifiedApplicationPath
        {
            get
            {
                //Return variable declaration
                var appPath = string.Empty;
        
                //Getting the current context of HTTP request
                var context = HttpContext.Current;
        
                //Checking the current context content
                if (context != null)
                {
                    //Formatting the fully qualified website url/name
                    appPath = string.Format("{0}://{1}{2}{3}",
                                            context.Request.Url.Scheme,
                                            context.Request.Url.Host,
                                            context.Request.Url.Port == 80
                                                ? string.Empty
                                                : ":" + context.Request.Url.Port,
                                            context.Request.ApplicationPath);
                }
        
                if (!appPath.EndsWith("/"))
                    appPath += "/";
        
                return appPath;
            }
        }
        

        【讨论】:

        • 完美运行。如果站点是server:8080/MySiteName,它会正确获取。
        • 感谢您分享实际代码而不是其他地方的链接。
        • context.Request.Url.Port == 80 会导致 HTTPS 出现问题
        • 注意!不适用于https。对于 https 需要将 context.Request.Url.Port == 80 替换为 (context.Request.Url.Port == 80 && context.Request.Url.Scheme == "http") || (context.Request.Url.Port == 443 && context.Request.Url.Scheme == "https") 或使用下面的答案
        • 也适用于本地主机(如果您正在本地测试)。如果不需要端口,可以使用 "http://" + HttpContext.Current.Request.Url.Host;
        【解决方案7】:
        string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
        

        Uri::GetLeftPart Method

        GetLeftPart 方法返回一个字符串,该字符串包含 URI 字符串的最左边部分,以 part 指定的部分结束。

        UriPartial Enumeration

        URI 的方案和权限段。

        【讨论】:

        • 比解析Url好多了!
        • 这应该是选择的答案。不必要的字符串操作过多。
        • 使用这个方法,http://www.lala.xxx/blah/blah会返回http://www.lala.xxx
        • +1 与 .Authority 不同——在我在 localhost 上进行的测试中——省略了协议 (http://) 部分。
        • UriPartial.Authority 可能会导致公共域 url 出现问题(在某些情况下,它会根据网络配置返回内部 url)
        【解决方案8】:
        string host = Request.Url.Host;
        Regex domainReg = new Regex("([^.]+\\.[^.]+)$");
        HttpCookie cookie = new HttpCookie(cookieName, "true");
        if (domainReg.IsMatch(host))
        {
          cookieDomain = domainReg.Match(host).Groups[1].Value;                                
        }
        

        【讨论】:

          【解决方案9】:

          我知道这是旧的,但现在这样做的正确方法是

          string Domain = HttpContext.Current.Request.Url.Authority
          

          这将获得带有服务器端口的 DNS 或 IP 地址。

          【讨论】:

            【解决方案10】:
            Match match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
            string domain = match.Groups[1].Success ? match.Groups[1].Value : null;
            

            host.com => 返回 host.com
            s.host.com => 返回 host.com

            host.co.uk => 返回 host.co.uk
            www.host.co.uk => 返回 host.co.uk
            s1.www.host.co.uk => 返回 host.co.uk

            【讨论】:

            • 我意识到这是一篇旧帖子,但 NQuenault 做得很好,我不擅长正则表达式,做得很好。正是我需要的。
            • @nquenault 关于如何最好地处理像 www.abc.com 这样的主机名有什么想法吗?谢谢!
            【解决方案11】:

            这将具体返回您所要求的内容。

            Dim mySiteUrl = Request.Url.Host.ToString()
            

            我知道这是一个较老的问题。但我需要同样简单的答案,这会返回所要求的内容(没有 http://)。

            【讨论】:

            • "没有 http://" 那么它不是"确切地问什么"...
            【解决方案12】:

            这也有效:

            字符串 url = HttpContext.Request.Url.Authority;

            【讨论】:

              【解决方案13】:

              如果示例 URL 是 http://www.foobar.com/Page1

              HttpContext.Current.Request.Url; //returns "http://www.foobar.com/Page1"
              
              
              HttpContext.Current.Request.Url.Host; //returns "www.foobar.com"
              
              
              HttpContext.Current.Request.Url.Scheme; //returns "http/https"
              
              
              HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); //returns "http://www.foobar.com"
              

              【讨论】:

              • 不不。 .Host"http://www.foobar.com/Page1"www.foobar.com,而不是 foobar.com
              • 是的,你是对的,更新了答案。 @tchelidze 谢谢
              【解决方案14】:

              --添加端口有助于运行 IIS Express

              Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
              

              【讨论】:

                猜你喜欢
                • 2012-04-26
                • 1970-01-01
                • 2014-11-29
                • 2014-09-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多