ASP.NET MVC 6应用程序的虚拟应用程序根路径

Zab*_*sky 2 asp.net-mvc asp.net-core-mvc

如何在服务器上获取应用程序的虚拟根路径?

换句话说:我如何在ASP.NET MVC 6中执行以下操作?

HttpContext.Current.Request.ApplicationPath

Dan*_*.G. 7

您需要的是什么@Url.Content("~/"),它将"〜"映射到您的虚拟应用程序根路径.

看看源代码,似乎使用HttpContext.Request.PathBase属性:

public virtual string Content(string contentPath)
{
    if (string.IsNullOrEmpty(contentPath))
    {
        return null;
    }
    else if (contentPath[0] == '~')
    {
        var segment = new PathString(contentPath.Substring(1));
        var applicationPath = HttpContext.Request.PathBase;

        return applicationPath.Add(segment).Value;
    }

    return contentPath;
}
Run Code Online (Sandbox Code Playgroud)