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.
Don't use a static class. You can keep something similar to your class, and register it as a singleton.
public class Common
{
private readonly IWebHostEnvironment _env;
public Common(IWebHostEnvironment env)
{
_env = env;
}
private void DeleteTestFiles()
{
var path = Path.Combine(_env.ContentRootPath, "App_Data/Files");
.....
}
}
Then, in Startup.ConfigureServices
:
services.AddSingleton<Common>();
Finally, inject Common
where you need it, instead of just statically referencing the class. This is how things work in DI, statics are a no-go.
UPDATE:
In order this to work, because the Utility project is referencing Microsoft.NETCore.App
and not Microsoft.AspNetCore.App
framework then you have to use IHostEnvironment
instead of IWebHostEnvironment
.
IWebHostEnvironment
actually implements IHostEnvironment
interface.
Did the test and all working good! I could get the ContentRootPath
.
public class Common
{
private readonly IHostEnvironment _env;
public Common(IHostEnvironment env)
{
_env = env;
}
private void DeleteTestFiles()
{
var path = Path.Combine(_env.ContentRootPath, "App_Data/Files");
.....
}
}
.Net 6 (.NetCore 3 and above) For example I want to locate ~/wwwroot/CSS
public class YourController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
public YourController (IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment= webHostEnvironment;
}
public IActionResult Index()
{
string webRootPath = _webHostEnvironment.WebRootPath;
string contentRootPath = _webHostEnvironment.ContentRootPath;
string path ="";
path = Path.Combine(webRootPath , "CSS");
//or path = Path.Combine(contentRootPath , "wwwroot" ,"CSS" );
return View();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With