Asp.net mvc ASP.NET MVC 6应用程序&x27;虚拟应用程序根路径

Asp.net mvc ASP.NET MVC 6应用程序&x27;虚拟应用程序根路径,asp.net-mvc,asp.net-core-mvc,Asp.net Mvc,Asp.net Core Mvc,如何获取服务器上应用程序的虚拟根路径 换句话说:在ASP.NET MVC 6中如何执行以下操作 您需要的内容可以通过@Url.Content(“~/”实现,它将“~”映射到您的虚拟应用程序根路径 查看一下,它似乎是使用HttpContext.Request.PathBase属性来实现的: public virtual string Content(string contentPath) { if (string.IsNullOrEmpty(contentPath)) {

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

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


您需要的内容可以通过
@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;
}

我在MVC Core RC2中使用以下内容:

我使用

Context.Request.PathBase+“/something”


或者更简单地说,只需使用
“/something”
,这意味着以斜杠开始的路径告诉asp核心从根开始。

检查@DanielJ.G.,我做了,它为您提供了指向project文件夹的绝对路径,这不是我所需要的。您不能只使用
@Url.Content(~/”
?我刚才也看了一下,它正在使用
HttpContext.Request.PathBase
@DanielJ.G.将“~”解析为应用程序路径,是的,我认为你是对的,尽管我找不到
PathBase
属性的正确规范。请张贴您的答案。谢谢。。。Context.Request.PathBase工作并提供了虚拟目录路径