In this article I will explain with an example, how to use IHostingEnvironment inside Controller in ASP.Net Core.
The IHostingEnvironment interface is injected into the Controller class using Dependency Injection in ASP.Net Core MVC.
What is IHostingEnvironment
The IHostingEnvironment is an interface for .Net Core 2.0.
The IHostingEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller.
The IHostingEnvironment interface have two properties.
1. WebRootPath – Path of the www folder.
2. ContentRootPath – Path of the root folder which contains all the Application files.
You will need to import the following namespace.
using Microsoft.AspNetCore.Hosting;
Using IHostingEnvironment
In the below example, the IHostingEnvironment is injected in the Controller and assigned to the private property Environment and later used to get the WebRootPath and ContentRootPath.
public class HomeController : Controller
    private IHostingEnvironment Environment;
    public HomeController(IHostingEnvironment _environment)
        Environment = _environment;
    public IActionResult Index()
        string wwwPath = this.Environment.WebRootPath;
        string contentPath = this.Environment.ContentRootPath;
        return View();
Using IHostingEnvironment inside Controller in ASP.Net Core