server.mappath equivalent in core 5.0 mvc Edit

Murugan Andezuthu Dharmaratnam | 26 February 2021 | 2332

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


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.