IWebHostEnvironment provides information about web hosting environments and The application is running.
Belongs to the namespace Microsoft.AspNetCore.Hosting
The IWebHostEnvironment interface needs to be injected as a dependency into controller and then used throughout the controller.
The IWebHostEnvironment interface has two properties.
We need to import the namespace
using Microsoft.AspNetCore.Hosting;
In the example below, IWebHostEnvironment is injected into the controller, and Assigned to the private property Environment, 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(); } }
The above is the detailed content of What is the role of the IWebHostEnvironment interface in C# ASP.NET Core?. For more information, please follow other related articles on the PHP Chinese website!