ASP.NET Core 3.0 HttpContext.Current.Server.MapPath
Asked Answered
S

2

15

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.

enter image description here

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.

Schilling answered 14/1, 2020 at 18:25 Comment(0)
N
4

.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();
    }
}
Narrows answered 12/4, 2022 at 11:32 Comment(0)
B
17

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");
                .....
        }
    }
Bogard answered 14/1, 2020 at 19:10 Comment(9)
Thanks a lot, but In case I want to get the IWebHostEnvironment instantiated in my static class is there a way? Like IWebHostEnvironment env = new something() ? Appreciate itSchilling
@Schilling what if you did HostingEnvironment env = new HostingEnvironment() ? both this class and IWebHostEnvironment implement IHostEnvironmentParol
No. It's not possible. That's why I suggested this approach. Statics are not going to work here, so you're just going to have to get over it.Bogard
@ChrisPratt thanks a lot, do you know what nuget package do I have to reference to my Utility project in order to get IWebHostEnvironment ?Schilling
I already tried to install different nuget packages and no one resolves the IWebHostEnvironment. And when right click "Quick actions and refactoring" it doesnt give me any install recomendation or something :(Schilling
Off the top of my head, no. However, it might require a framework reference. .NET Core 3.0 moved to using a framework reference, rather than package references for most components. If that is the case, you'll have to target .NET Core 3.1. That should not be an issue as any code that interacts with ASP.NET Core components will only be applicable to .NET Core targets, anyways, now.Bogard
Yeah, its already targeted to 3.1 but the framework is Microsoft.NetCore.App, I think i have to add Microsoft.AspNetCore.App framework in order to get the assembly. I will try to find out how to add a new frameworkSchilling
I already tried everything and cant get IWebHostEnvironment resolved in this project, and is targeted 3.1 :( I really appreciate any help I might be doing something horrible wrong.Schilling
Ok found the way, actually if the project is a class library and not the asp.net app, you cant uset IWebHostEnvironment, you have to use IHostEnvironment, because IWebHostEnvironment implements IHostEnvironment. I just ran the test and all amazing good.Schilling
N
4

.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();
    }
}
Narrows answered 12/4, 2022 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.