简体   繁体   中英

Create HostingEnvironment instance with WebRootPath

I'm migrating from netcore2.1 to netcore3.1 . I have a service where I need to create an IServiceProvider and inject IWebHostEnvironment (I used to inject IHostingEnvironment in netcore2.1). The code used to just create a new Microsoft.Extensions.Hosting.Internal.HostingEnvironment instance but that class doesn't contain WebRootPath anymore. I fetched git repo and tried to look for implementations of IWebHostEnvironment but it seems that the only implementation is internal to aspnetcore which is of no use.

Is there a possibility to create an instance of IWebHostEnvironment somehow?

Daniel

Hi You can access WebRoot Path using in Core 3.1

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    public class HomeController : Controller
    {
          private readonly IWebHostEnvironment _hostingEnv;

           public HomeController(IWebHostEnvironment hostingEnv)
           {
             _hostingEnv = hostingEnv;
           }

          private Void GetWebRoot()
          {
             var webRootPath = _hostingEnv.WebRootPath;
          }
     }

Can you try this, upvote if it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM