代码之家  ›  专栏  ›  技术社区  ›  core

如何在ASP.NET中获取根域URI?

  •  87
  • core  · 技术社区  · 17 年前

    假设我在 http://www.foobar.com .

    我有没有办法用程序来确定 http://www.foobar.com/ “在我的代码背后(即不必在我的web配置中硬编码)?

    14 回复  |  直到 7 年前
        1
  •  79
  •   JoshBerke    17 年前

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

        2
  •  152
  •   d.danailov Felix Kling    11 年前
    string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
    

    Uri::GetLeftPart Method :

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

    UriPartial Enumeration :

    URI的方案和权限段。

        3
  •  121
  •   Brian Hasden    14 年前

    对于仍在疑惑的人,可以在以下网址找到更完整的答案: 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;
        }
    }
    
        4
  •  27
  •   Dheeraj Palagiri    7 年前

    如果示例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"
    
        5
  •  26
  •   Ben Lesh    17 年前
    string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"
    
        6
  •  16
  •   Rex M    17 年前

    要获取整个请求URL字符串:

    HttpContext.Current.Request.Url
    

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

    HttpContext.Current.Request.Url.Host
    

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

        7
  •  4
  •   JonathanC    11 年前

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

    Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
    
        8
  •  3
  •   Sk93    17 年前
    string domainName = Request.Url.Host
    
        9
  •  3
  •   idlehands23    13 年前

    我知道这是老的,但现在正确的方法是

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

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

        10
  •  3
  •   NQuenault    13 年前
    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

        11
  •  3
  •   Alexander_F    11 年前

    这也适用于:

    字符串url=httpcontext.request.url.authority;

        12
  •  1
  •   Saul Dolgin    17 年前

    C示例如下:

    string scheme = "http://";
    string rootUrl = default(string);
    if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "on")
    {
      scheme = "https://";
    }
    rootUrl = scheme + Request.ServerVariables["SERVER_NAME"].ToString();
    
        13
  •  1
  •   Shawn Constance    14 年前
    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;                                
    }
    
        14
  •  1
  •   abatishchev Karl Johan    11 年前

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

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

    我知道这是个老问题。但我需要同样简单的答案,这将返回被询问的内容(不带http://)。

    推荐文章