简体   繁体   English

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

[英]ASP.NET MVC 6 application's virtual application root path

How do I get the virtual root path of the application on the server? 如何在服务器上获取应用程序的虚拟根路径?

In another words: how can I do the following in ASP.NET MVC 6? 换句话说:我如何在ASP.NET MVC 6中执行以下操作?

HttpContext.Current.Request.ApplicationPath HttpContext.Current.Request.ApplicationPath

What you need can be achieved with @Url.Content("~/") , which will map "~" to your virtual application root path. 您需要的是@Url.Content("~/") ,它将“〜”映射到您的虚拟应用程序根路径。

Having a look at the source code , it seems to do so using the HttpContext.Request.PathBase property: 看一下源代码 ,似乎是使用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;
}

I use the following in MVC Core RC2: 我在MVC Core RC2中使用以下内容:

Instead of "~/something" I use 而不是我使用的"~/something"

Context.Request.PathBase + "/something" Context.Request.PathBase +“/ something”

or even more simply, just use "/something" , which means starting a path with a slash tells asp core to start from the root. 或者更简单地说,只需使用"/something" ,这意味着使用斜杠启动路径会告诉asp核心从根开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ASP.NET MVC根应用程序路由到MVC虚拟目录 - Routing ASP.NET MVC root application to MVC virtual directory 为什么当前的ASP.NET MVC应用程序根虚拟路径在另一个线程/任务中不可用? - Why current ASP.NET MVC application root virtual path is not available in a another thread/Task? 500.22用于带有子虚拟目录ASP.NET MVC应用程序的根ASP.NET应用程序上的子目录文件 - 500.22 for subdirectories files on root ASP.NET application with child virtual directory ASP.NET MVC application ASP.NET MVC应用程序应用程序根目录问题 - asp.net mvc application application root issue 在具有不同根相对路径的代理后面运行ASP.NET MVC应用程序 - Running ASP.NET MVC application behind a proxy with different root relative path 如何解析相对于ASP.NET MVC 4应用程序根目录的路径? - How do I resolve a path relative to an ASP.NET MVC 4 application root? 在ASP.NET MVC应用程序下将ASP.NET Web API应用程序配置为虚拟目录 - Configure ASP.NET Web API application as a Virtual Directory under ASP.NET MVC application 获取ASP.Net中的当前应用程序虚拟路径 - Get The Current Application Virtual Path in ASP.Net 在单个虚拟目录中部署多个ASP.NET MVC应用程序 - Deploying multiple ASP.NET MVC application in a single Virtual directory Umbraco 根站点和子目录中的 ASP.NET MVC 应用程序 - Umbraco root site and ASP.NET MVC application in subdirectory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM