I know I can get WebRoot by HostingEnvironment (Microsoft.AspNet.Hosting namespace).
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?
EDIT:
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.
Oct 4, 2015
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.
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();
}
On Azure Web Apps, you'll find the wwwroot
is in a different place:
All of the source code gets copied to D:/Home/site/approot/src/{WebsiteName}
except the wwwroot
. IApplicationEnvironment.ApplicationBasePath
points here.
The wwwroot
goes in D:/Home/site/wwwroot
. IHostingEnvironment.WebRootPath
points here.
It still won't work with IIS virtual directories. There is a WebRootFileProvider
property on the IHostingEnvironment
interface which provides an IFileProvider
with methods like GetFileInfo(string subpath)
. I read the code and found subpath
is a physical one, not a virtual one.
Obviously, in a self-hosted setup, there is no IIS. If you rely on IIS and really must do this, you'll probably have to reference System.Web.
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With