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

时间:2009-07-31 20:09:44

标签: asp.net url

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

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

14 个答案:

答案 0 :(得分:157)

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

Uri::GetLeftPart Method

  

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

UriPartial Enumeration

  

URI的方案和权限段。

答案 1 :(得分:121)

对于仍在疑惑的人,可以在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;
    }
}

答案 2 :(得分:80)

HttpContext.Current.Request.Url可以获取有关该网址的所有信息。并且可以将网址分解成碎片。

答案 3 :(得分:27)

string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"

答案 4 :(得分:27)

如果示例网址为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 :(得分:16)

获取整个请求URL字符串:

HttpContext.Current.Request.Url

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

HttpContext.Current.Request.Url.Host

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

答案 6 :(得分:4)

- 在运行IIS Express时添加端口可以提供帮助

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

答案 7 :(得分:3)

string domainName = Request.Url.Host

答案 8 :(得分:3)

我知道这已经过时了,但现在正确的方法是

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

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

答案 9 :(得分:3)

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 =>返回host.co.uk
www.host.co.uk =>返回host.co.uk
s1.www.host.co.uk =>返回host.co.uk

答案 10 :(得分:3)

这也有效:

  

string url = HttpContext.Request.Url.Authority;

答案 11 :(得分:1)

下面的C#示例:

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

答案 12 :(得分:1)

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;                                
}

答案 13 :(得分:1)

这将特别返回您的要求。

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

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