Missing AspNetCore Nuget Packages
There are several aspects of an ASP Net Core web API that cannot be placed in a class library without a little extra care. This is because Microsoft decided to remove “many of the core assemblies” from nuget packages for the ASPNetCore, when Net Core 3.0 was published, and has not been restored as of NET 6.0.
See this reference for why: Use ASP.NET Core APIs in a class library
So, if you run into a situation where you are trying to create, for example, a Controller class in a class library project, you will not find any Nuget package for the ControllerBase class (in the Microsoft.ASPNetCore.MVC) namespace.
To rectify this, you must manually add a framework reference to the project.cs file that points to “Microsoft.AspNetCore.App”.
This is what the project.cs file would look like with the framework reference added:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
This will need to be done for Controller classes that derive from the base, as well as, references to the IWebHostEnvironment environment.
No Comments