Get wwwroot folder path from ASP.NET 5 controller VS 2015
You will need to inject IWebHostEnvironment
into your class to have access to the ApplicationBasePath
property value: Read about Dependency Injection. After successfully injecting the dependency, the wwwroot path should be available to you. For example:
private readonly IWebHostEnvironment _appEnvironment;
public ProductsController(IWebHostEnvironment appEnvironment)
{
_appEnvironment = appEnvironment;
}
Usage:
[HttpGet]
public IEnumerable<string> Get()
{
FolderScanner scanner = new FolderScanner(_appEnvironment.WebRootPath);
return scanner.scan();
}
Edit: IHostingEnvironment
has been replaced by IWebHostEnvironment
in later versions of asp.net.
I know this has already been answered, but it has given me different results depending on my hosting environment (IIS Express vs IIS). The following approach seems to work for all hosting environments nicely if you want to get your wwwroot path (see this GitHub issue).
For example
private readonly IHostingEnvironment _hostEnvironment;
public ProductsController(IHostingEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}
[HttpGet]
public IEnumerable<string> Get()
{
FolderScanner scanner = new FolderScanner(_hostEnvironment.WebRootPath);
return scanner.scan();
}