简体   繁体   English

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

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

Let's say I'm hosting a website at http://www.foobar.com . 假设我在http://www.foobar.com上托管了一个网站。

Is there a way I can programmatically ascertain " http://www.foobar.com/ " in my code behind (ie without having to hardcode it in my web config)? 有没有办法可以在我的代码中以编程方式确定“ http://www.foobar.com/ ”(即无需在我的Web配置中对其进行硬编码)?

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

Uri::GetLeftPart Method : Uri :: GetLeftPart方法

The GetLeftPart method returns a string containing the leftmost portion of the URI string, ending with the portion specified by part. GetLeftPart方法返回一个字符串,其中包含URI字符串的最左边部分,以part指定的部分结束。

UriPartial Enumeration : UriPartial枚举

The scheme and authority segments of the URI. URI的方案和权限段。

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/ . 对于仍然想知道的人,可以在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;
    }
}

HttpContext.Current.Request.Url can get you all the info on the URL. HttpContext.Current.Request.Url可以获取有关URL的所有信息。 And can break down the url into its fragments. 并且可以将url分解成碎片。

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

If example Url is http://www.foobar.com/Page1 如果示例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"

To get the entire request URL string: 要获取整个请求URL字符串:

HttpContext.Current.Request.Url

To get the www.foo.com portion of the request: 要获得请求的www.foo.com部分:

HttpContext.Current.Request.Url.Host

Note that you are, to some degree, at the mercy of factors outside your ASP.NET application. 请注意,您在某种程度上受ASP.NET应用程序之外的因素的影响。 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. 如果IIS配置为接受应用程序的多个或任何主机标头,那么通过DNS解析到您的应用程序的任何域可能会显示为请求URL,具体取决于用户输入的那个。

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

Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
string domainName = Request.Url.Host

I know this is older but the correct way to do this now is 我知道这个比较老但现在这样做的正确方法是

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

That will get the DNS or ip address with port for a server. 这将获得带有服务器端口的DNS或IP地址。

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 host.com =>返回host.com
s.host.com => return host.com s.host.com =>返回host.com

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

This works also: 这也有效:

string url = HttpContext.Request.Url.Authority; string url = HttpContext.Request.Url.Authority;

C# Example Below: C#示例如下:

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

This will return specifically what you are asking. 这将特别返回您的要求。

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

I know this is an older question. 我知道这是一个较老的问题。 But I needed the same simple answer and this returns exactly what is asked (without the http://). 但是我需要相同的简单答案,这将完全返回所要求的内容(没有http://)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM