如何获取站点根URL? [英] How do I get the site root URL?
本文介绍了如何获取站点根URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得一个ASP.NET应用程序的绝对根URL动态。这需要全根URL到表单应用程序有:HTTP:/
I want to get the absolute root Url of an ASP.NET application dynamically. This needs to be the full root url to the application in the form: http(s)://hostname(:port)/
我一直用这个静态方法:
I have been using this static method:
public static string GetSiteRootUrl()
{
string protocol;
if (HttpContext.Current.Request.IsSecureConnection)
protocol = "https";
else
protocol = "http";
StringBuilder uri = new StringBuilder(protocol + "://");
string hostname = HttpContext.Current.Request.Url.Host;
uri.Append(hostname);
int port = HttpContext.Current.Request.Url.Port;
if (port != 80 && port != 443)
{
uri.Append(":");
uri.Append(port.ToString());
}
return uri.ToString();
}
但是,如果我没有 HttpContext.Current
的范围?
我也遇到过这种情况 CacheItemRemovedCallback
。
推荐答案
我已经通过添加的AppSettings(SiteRootUrl)一个web.config设置解决了这个。简单有效,但另一个配置设置来维持。
I've solved this by adding a web.config setting in AppSettings ("SiteRootUrl"). Simple and effective, but yet another config setting to maintain.
这篇关于如何获取站点根URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文