What is the role of IWebHostEnvironment interface in C# ASP.NET Core?

IWebHostEnvironment Provides information about the web hosting environment an application is running in.

belongs to namespace Microsoft.AspNetCore.Hosting

The IWebHostEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller.

The IWebHostEnvironment interface have two properties.

  • WebRootPath − Path of the www folder(Gets or sets the absolute path to the directory that contains the web-servable application content files)
  • ContentRootPath − Path of the root folder which contains all the Application files(Gets or sets an IFileProvider pointing at WebRootPath.)


We need to import namesace

using Microsoft.AspNetCore.Hosting;

In the below example, the IWebHostEnvironment 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 IWebHostEnvironment Environment;
   public HomeController(IWebHostEnvironment _environment){
      Environment = _environment;
   public IActionResult Index(){
      string wwwPath = this.Environment.WebRootPath;
      string contentPath = this.Environment.ContentRootPath;
      return View();

Updated on: 25-Sep-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started