s

server.mappath equivalent in asp.net core 5.0 mvc edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 26 February 2021 | 3260

Web application developers who code in classic asp, asp .net, and asp .net MVC have always relied on server.mappath to resolve file paths. But this method is no longer available in asp .net core 5.0 MVC. The is to use IHostEnvironment

Solution

The solution is to inject IHostEnvironment in either the HomeController or other Controllers or in the startup.cs file.

public Startup(IConfiguration configuration, IHostEnvironment HostEnvironment)
{
    Common.WebRootPath = HostEnvironment.ContentRootPath;
}


The ContentRootPath resolves the application's base path.