【发布时间】:2010-11-15 22:08:24
【问题描述】:
假设我在http://www.foobar.com 托管一个网站。
有没有一种方法可以在我的代码中以编程方式确定“http://www.foobar.com/”(即无需在我的网络配置中对其进行硬编码)?
【问题讨论】:
-
由于这取决于请求,您可以尝试查看
Request对象。
假设我在http://www.foobar.com 托管一个网站。
有没有一种方法可以在我的代码中以编程方式确定“http://www.foobar.com/”(即无需在我的网络配置中对其进行硬编码)?
【问题讨论】:
Request 对象。
HttpContext.Current.Request.Url 可以为您获取 URL 上的所有信息。并且可以将url分解成片段。
【讨论】:
获取整个请求 URL 字符串:
HttpContext.Current.Request.Url
获取请求的 www.foo.com 部分:
HttpContext.Current.Request.Url.Host
请注意,在某种程度上,您会受到 ASP.NET 应用程序之外的因素的影响。如果 IIS 配置为您的应用程序接受多个或任何主机标头,则通过 DNS 解析到您的应用程序的任何域都可能显示为请求 URL,具体取决于用户输入的 URL。
【讨论】:
string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"
【讨论】:
下面的 C# 示例:
string scheme = "http://";
string rootUrl = default(string);
if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "on")
{
scheme = "https://";
}
rootUrl = scheme + Request.ServerVariables["SERVER_NAME"].ToString();
【讨论】:
string domainName = Request.Url.Host
【讨论】:
对于仍有疑问的任何人,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;
}
}
【讨论】:
context.Request.Url.Port == 80 替换为 (context.Request.Url.Port == 80 && context.Request.Url.Scheme == "http") || (context.Request.Url.Port == 443 && context.Request.Url.Scheme == "https") 或使用下面的答案
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
GetLeftPart 方法返回一个字符串,该字符串包含 URI 字符串的最左边部分,以 part 指定的部分结束。
URI 的方案和权限段。
【讨论】:
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;
}
【讨论】:
我知道这是旧的,但现在这样做的正确方法是
string Domain = HttpContext.Current.Request.Url.Authority
这将获得带有服务器端口的 DNS 或 IP 地址。
【讨论】:
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
【讨论】:
这将具体返回您所要求的内容。
Dim mySiteUrl = Request.Url.Host.ToString()
我知道这是一个较老的问题。但我需要同样简单的答案,这会返回所要求的内容(没有 http://)。
【讨论】:
这也有效:
字符串 url = HttpContext.Request.Url.Authority;
【讨论】:
如果示例 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"
【讨论】:
.Host 的 "http://www.foobar.com/Page1" 是 www.foobar.com,而不是 foobar.com。
--添加端口有助于运行 IIS Express
Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
【讨论】: