How can I get the root domain URI in ASP.NET?

后端 未结 14 1432
庸人自扰
庸人自扰 2020-12-12 16:15

Let\'s say I\'m hosting a website at http://www.foobar.com.

Is there a way I can programmatically ascertain \"http://www.foobar.com/\" in my code behind (i.e. witho

相关标签:
14条回答
  • 2020-12-12 16:26
    Match match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
    string domain = match.Groups[1].Success ? match.Groups[1].Value : null;
    

    host.com => return host.com
    s.host.com => return host.com

    host.co.uk => return host.co.uk
    www.host.co.uk => return host.co.uk
    s1.www.host.co.uk => return host.co.uk

    0 讨论(0)
  • 2020-12-12 16:27
    string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"
    
    0 讨论(0)
  • 2020-12-12 16:30
    string domainName = Request.Url.Host
    
    0 讨论(0)
  • 2020-12-12 16:31
    string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
    

    Uri::GetLeftPart Method:

    The GetLeftPart method returns a string containing the leftmost portion of the URI string, ending with the portion specified by part.

    UriPartial Enumeration:

    The scheme and authority segments of the URI.

    0 讨论(0)
  • 2020-12-12 16:31

    To get the entire request URL string:

    HttpContext.Current.Request.Url
    

    To get the www.foo.com portion of the request:

    HttpContext.Current.Request.Url.Host
    

    Note that you are, to some degree, at the mercy of factors outside your ASP.NET application. If IIS is configured to accept multiple or any host header for your application, then any of those domains which resolved to your application via DNS may show up as the Request Url, depending on which one the user entered.

    0 讨论(0)
  • 2020-12-12 16:38

    For anyone still wondering, a more complete answer is available at 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题