[英]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.