- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.)
Usage
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.
Example
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(); } }
Advertisements