ASP.NET Core 3.1在单独的类库中获取wwwroot路径 [英] ASP.NET Core 3.1 Get wwwroot Path In Separate Class Library

查看:250
本文介绍了ASP.NET Core 3.1在单独的类库中获取wwwroot路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我可以使用DI访问 IHostingEnvironment 并将其传递到单独的类库中以获得wwwroot路径,但是在3.1版中, IHostingEnvironment 已被弃用,建议使用使用 IWebHostEnvironment .在我的一生中,我找不到要添加的NuGet包来访问该对象.我试过 Microsoft.AspNetCore.Hosting Microsoft.AspNetCore.Hosting.Abstractions 都没有运气.任何人?如果使用此方法不再可能,有人可以提出解决方案,以便我可以将路径映射到我的应用程序的wwwroot文件夹吗?

Previously, I could access IHostingEnvironment using DI and pass that into my separate class library to get the wwwroot path, but in 3.1, IHostingEnvironment is being deprecated and it's suggested to use IWebHostEnvironment. For the life of me, I can't find a NuGet package to add to gain access to that object. I've tried Microsoft.AspNetCore.Hosting and Microsoft.AspNetCore.Hosting.Abstractions with no luck. Anyone? If it's no longer possible using this method, can someone propose a solution so that I can map a path to the wwwroot folder for my application?

推荐答案

通过链接查看文档的链接是一个答案,但是由于链接可能会消失,因此这是解决方案.

The link by poke about seeing the documentation is an answer but since links can die, here is the solution.

从.NET Core 3.0开始,使用Microsoft.NET.Sdk.Web MSBuild SDK的项目隐式引用了共享框架.使用Microsoft.NET.Sdk或Microsoft.NET.Sdk.Razor SDK的项目必须引用ASP.NET Core才能在共享框架中使用ASP.NET Core API.

As of .NET Core 3.0, projects using the Microsoft.NET.Sdk.Web MSBuild SDK implicitly reference the shared framework. Projects using the Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference ASP.NET Core to use ASP.NET Core APIs in the shared framework.

在您的项目文件中,添加此...

In you project file, add this...

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

这篇关于ASP.NET Core 3.1在单独的类库中获取wwwroot路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆