

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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(); } }
- Related Questions & Answers
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is ViewData in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- Explain the role of HttpContext class in ASP.NET Core
- How to determine if C# .NET Core is installed?
- How to use ViewBag in ASP .Net MVC C#?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- What is the role of pageYOffset property?
- What is the use of UseIISIntegration in C# Asp.net Core?
- What is core JavaScript language?
- What is the role of Compiler Construction Tools?
- What is the role of C++ in Computer Science?
- What is the role of clearTimeout() function in JavaScript?
- What is the role of throw statement in JavaScript?
Advertisements