ASP.NET MVC 6应用程序的虚拟应用程序根路径 [英] ASP.NET MVC 6 application's virtual application root path
本文介绍了ASP.NET MVC 6应用程序的虚拟应用程序根路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取服务器上应用程序的虚拟根路径?
How do I get the virtual root path of the application on the server?
换句话说:如何在ASP.NET MVC 6中执行以下操作?
In another words: how can I do the following in ASP.NET MVC 6?
推荐答案
您可以使用
@Url.Content("~/")
实现所需的功能,它将〜"映射到您的虚拟应用程序根路径.What you need can be achieved with
@Url.Content("~/")
, which will map "~" to your virtual application root path.Having a look at the source code, it seems to do so using the
HttpContext.Request.PathBase
property:
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; }
这篇关于ASP.NET MVC 6应用程序的虚拟应用程序根路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文