Im migration a classic C# MVC
project to .NET Core
and I have a Utility Project where I need to get access of App_Data
folder.
I already create in this new project my App_Data
folder outside wwwroot
but I need to get reference to it from this Utility Project.
This is my old code:
public static class Common
{
private static void DeleteTestFiles()
{
var path = HttpContext.Current.Server.MapPath("~/App_Data/Files");
.....
}
}
I was reading that in 3.0 there is a way to do this, here the example:
private readonly IWebHostEnvironment _hostingEnvironment;
public HomeController(IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public ActionResult Index()
{
string webRootPath = _hostingEnvironment.WebRootPath;
string contentRootPath = _hostingEnvironment.ContentRootPath;
return Content(webRootPath + "\n" + contentRootPath);
}
But above code is for a Controller that lives in the web project. My Utility Projects common class is not a Controller and is a static class so I cannot have constructor in order to have IWebHostEnvironment
to be injected automaticly.
Any clue how can I do get the path or maybe I need to manually inject IWebHostEnvironment but don't have a clue.