如何映射到物理路径的虚拟路径?

问题描述:

我知道我可以通过HostingEnvironment(Microsoft.AspNet.Hosting命名空间)得到的WebRoot。

I know I can get WebRoot by HostingEnvironment (Microsoft.AspNet.Hosting namespace).

我需要根据我的web应用程序中在IIS中创建一个虚拟路径得到的物理路径。在IIS中,在网站根目录指向我的出版网站的wwwroot文件,有一个虚拟目录在IIS中,它指向的文件夹我的wwwroot以外的补充。我希望我能得到的虚拟目录的物理路径。在MVC 5或更早的版本,我可以使用HostingEnvironment.MapPath(System.Web命名),或者使用Server.Mappath,我应该怎么在MVC 6办?

I need to get a physical path according to a virtual path created in IIS within my web application. In IIS, the website root points to wwwroot of my published site and there is a virtual directory added in IIS which points to a folder outside of my wwwroot. I hope I can get the physical path of that virtual directory. In MVC 5 or earlier version, I can use HostingEnvironment.MapPath (System.Web namespace) or Server.MapPath, what should I do in MVC 6?

编辑:

这不是虚拟路径,但在IIS中添加虚拟目录。我希望我可以得到的虚拟目录的物理路径。我认为,虚拟目录是IIS的特定功能,它看起来像一个完整的虚拟路径的子路径,但可以是物理Web根目录以外的文件夹中。

It's not the virtual path but the virtual directory added in IIS. I hope I can get the physical path of that virtual directory. I think virtual directory is a particular feature of IIS, which looks like a sub path of a full virtual path but can be a folder outside of physical web root folder.

2015年10月4日

参照的这个问题在ASP.NET主机5 回购,到目前为止,它似乎并没有我们可以得到在IIS虚拟目录的物理路径。

Refer to this issue in ASP.NET 5 Hosting repo, So far, it doesn't seem we can get the physical path of a virtual directory in IIS.

您可以使用 IApplicationEnvironment 为,其中包含属性 ApplicationBasePath

You can use IApplicationEnvironment for that, which contains the property ApplicationBasePath

    private readonly IApplicationEnvironment _appEnvironment;

    public HomeController(IApplicationEnvironment appEnvironment)
    {
        _appEnvironment = appEnvironment;
    }

    public IActionResult Index()
    {
        var rootPath = _appEnvironment.ApplicationBasePath;
        return View();
    }