As you know in ASP .NET & ASP .NET MVC application Server.MapPath is used, but in .NET Core application Microsoft has permanently removed Server.MapPath function and introduced a new interfaces IHostingEnvironment for .Net Core 2.0 and IWebHostEnvironment for .Net Core 3.0 and onwards.
Difference of IHostingEnvironment and IWebHostEnvironment
The IHostingEnvironment is an interface for .Net Core 2.0 and IWebHostEnvironment has replaced IHostingEnvironment in .Net Core 3.0.
Both these interfaces need to be injected as dependency in the Controller and then later used throughout the Controller. Both these interfaces have two properties.
- WebRootPath – Path of the www folder ( using this path you can access all static files)
- ContentRootPath – Path of the root folder which contains all the Application files. ( using this you can get all the application file in root folder)
Before using IHostingEnvironment and IWebHostEnvironment you need to import the below namespace.
The IHostingEnvironment is injected in the Controller and assigned to the private property Environment and then used to get the WebRootPath and ContentRootPath as on below;
When you run the application you can see the result as below;
A warning was showing to use IWebHostEnvironment as we create application in target framework 3.1 |
The IWebHostEnvironment is injected in the Controller and assigned to the private property Environment and then used to get the WebRootPath and ContentRootPath as on below;
Summary
You May Also Like...
- Send email with attachment in .NET Core
- Difference between AddSingleton vs AddScoped vs AddTransient in asp.net core
- Dependency Injection in Asp.Net Core
- How to read values from appsettings.json in Asp.Net Core
- How to Get Application Base Path in Asp.Net Core
- Import and Export Excel file in ASP .NET Core 3.1 razor page
- Consume Restful API to get the bank details using IFSC Code in .NET Core
- Create ASP .NET Core MVC application using VS Code
- Generate QR Code in .NET Core using Bitmap
- How to Securely Open PDF in Browser using .NET Core
- Binding Dropdown List With Database in ASP.NET Core and Entity Framework
Post a Comment