Reputation: 4229
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?
Upvotes: 8
Views: 4048
Reputation: 11905
To expand on John81's answer a bit: The relevant section of the Microsoft docs state:
To reference ASP.NET Core, add the following
<FrameworkReference>
element to your project file:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp3.0</TargetFramework> </PropertyGroup> <ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> </ItemGroup> </Project>
Referencing ASP.NET Core in this manner is only supported for projects targeting .NET Core 3.x.
Note the TargetFramework
is netcoreapp3.0
, which complies with the statement above that this only works for projects targeting .NET Core 3.x. Most library projects target some version of netstandard, so you need to change the <targetFramework>
element in addition to adding the <FrameworkReference>
.
Upvotes: 2
Reputation: 119
From documentation. This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment.
Upvotes: 0
Reputation: 4084
The link by poke about seeing the documentation is an answer but since links can die, here is the solution.
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>
Upvotes: 4