30

While ASP.NET Core up to 2.2 could be consumed through NuGet to create library projects for shared Controllers, Middleware etc, how do I create a library that is able to use ASP.NET Core 3.0 types?

While for projects containing views there is a "Razor Class Library" (razorclasslib) template, how do I create a library that only contains logic components?

1 Answer 1

40

Applications built for .NET Core 3.0 can reference one or more shared frameworks. ASP.NET Core is one of these shared frameworks (others would be the base .NET Core Shared framework and the Windows Desktop Shared Framework containing WinForms and WPF).

To reference ASP.NET Core from a classic .NET Core library targeting .NET Core 3.0 (netcoreapp3.0, not .NET Standard), you can use a FrameworkReference in the csproj to reference the framework:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

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

</Project>

When opened in Visual Studio, this additional framework reference will show up in the dependencies node in solution explorer:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Yes that works and what's cool about this is that if you add it and only reference one or two things out of it you can build a trimmed EXE/Package and it'll pick out only what's needed, unlike explicit assembly refs. I needed IWebHostEnvironment only and the added footprint is tiny. Thanks!
and what if the library targets .net standard 2.1?
Then you cannot use asp.net core APIs and need to retarget to netcoreapp3.0. Only EF Core and Microsoft.Extensions packages of the 3.0 releases can be used in .NET Standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.