How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac
See the question and my original answer on StackOverflowIf that can be useful to anyone, in a Razor Page cshtml.cs file, here is how to get it: add an IHostEnvironment hostEnvironment
parameter to the constructor and it will be injected automatically:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IHostEnvironment _hostEnvironment;
public IndexModel(ILogger<IndexModel> logger, IHostEnvironment hostEnvironment)
{
_logger = logger;
_hostEnvironment = hostEnvironment; // has ContentRootPath property
}
public void OnGet()
{
}
}
PS: IHostEnvironment
is in Microsoft.Extensions.Hosting
namespace, in Microsoft.Extensions.Hosting.Abstractions.dll
... what a mess!