我如何获得ASP.NET中的根域URI?



url (10)

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

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

https://src-bin.com

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

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


Answer #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();

Answer #2

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

HttpContext.Current.Request.Url; //returns "http://www.foobar.com/Page1"


HttpContext.Current.Request.Url.Host; //returns "foobar.com"


HttpContext.Current.Request.Url.Scheme; //returns "http/https"


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

Answer #3

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

Answer #4

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

HttpContext.Current.Request.Url

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

HttpContext.Current.Request.Url.Host

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


Answer #5

这也适用于:

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


Answer #6
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


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

Uri :: GetLeftPart方法

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

UriPartial枚举

URI的方案和权限片段。


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

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




url