简体   繁体   English

使用 WebRootPath 创建 HostingEnvironment 实例

[英]Create HostingEnvironment instance with WebRootPath

I'm migrating from netcore2.1 to netcore3.1 .我正在从netcore2.1迁移到netcore3.1 I have a service where I need to create an IServiceProvider and inject IWebHostEnvironment (I used to inject IHostingEnvironment in netcore2.1).我有一个服务,我需要在其中创建一个 IServiceProvider 并注入IWebHostEnvironment (我曾经在IHostingEnvironment中注入 IHostingEnvironment)。 The code used to just create a new Microsoft.Extensions.Hosting.Internal.HostingEnvironment instance but that class doesn't contain WebRootPath anymore.用于创建新Microsoft.Extensions.Hosting.Internal.HostingEnvironment实例的代码,但该类不再包含WebRootPath 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.我获取了 git repo 并试图寻找 IWebHostEnvironment 的实现,但似乎唯一的实现是在 aspnetcore 内部没有用的。

Is there a possibility to create an instance of IWebHostEnvironment somehow?是否有可能以某种方式创建 IWebHostEnvironment 的实例?

Daniel丹尼尔

Hi You can access WebRoot Path using in Core 3.1嗨,您可以在 Core 3.1 中使用 WebRoot Path

    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你可以试试这个,如果有帮助就点赞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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