如何获取根URL路径

9
3个回答

3

在JavaScript中,您可以从location对象中获取URL信息。

var href = location.href; //returns the entire url
var host = location.hostname; //returns just the hostname of the url.

使用 href = http://localhost:52390/pages/user.aspx; 和 host = localhost。 - MiBol
没错。一旦你有了这个信息,就可以很容易地拆分字符串来获取你需要的部分。 - Troy Harris

2

ASPX:Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

Javascript实现:

<script type="text/javascript">
    var rootUrl = '<% =(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath) %>';
</script>

0

有时候我需要从代码后台获取根路径。这是我使用的方法。

public static string GetRootPath()
{
    HttpContext CTX;
    CTX = HttpContext.Current;
    return CTX.Request.ApplicationPath.ToString();
}

那是 JavaScript 吗? - parsecer
C#。发帖者询问如何在JavaScript或APX(.NET Forms)中实现此功能。这将在服务器端调用期间获取路径。 - Elim Garak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接