我正在寻找一种获取Web项目根URL的方法,例如:
本地:
http://localhost:52390/pages/user.aspx
http://localhost:52390/pages/user.aspx
在JavaScript中,您可以从location对象中获取URL信息。
var href = location.href; //returns the entire url
var host = location.hostname; //returns just the hostname of the url.
ASPX:Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
Javascript实现:
<script type="text/javascript">
var rootUrl = '<% =(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath) %>';
</script>
有时候我需要从代码后台获取根路径。这是我使用的方法。
public static string GetRootPath()
{
HttpContext CTX;
CTX = HttpContext.Current;
return CTX.Request.ApplicationPath.ToString();
}