Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
By Scott Addie
Use ASP.NET Core APIs in a class library to share Razor components, MVC features, and other web functionality across apps. This article explains how to reference the shared framework and configure libraries for supported ASP.NET Core versions. For all other library guidance, see Open-source library guidance.
Determine which ASP.NET Core versions to support
ASP.NET Core adheres to the .NET and .NET Core support policy. Consult the support policy when determining which ASP.NET Core versions to support in a library. A library should:
- Make an effort to support all ASP.NET Core versions classified as Long-Term Support (LTS).
- Not feel obligated to support ASP.NET Core versions classified as End of Life (EOL).
As preview releases of ASP.NET Core are made available, breaking changes are posted in the aspnet/Announcements GitHub repository. Compatibility testing of libraries can be conducted as framework features are being developed.
Use the ASP.NET Core shared framework
With the release of .NET Core 3.0, many ASP.NET Core assemblies are no longer published to NuGet as packages. Instead, the assemblies are included in the Microsoft.AspNetCore.App shared framework, which is installed with the .NET SDK and runtime installers.
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.
To reference ASP.NET Core, add the following <FrameworkReference> element to your project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
Include Blazor extensibility
Blazor supports creating Razor components class libraries for server-side and client-side apps. To support Razor components in a class library, the class library must use the Microsoft.NET.Sdk.Razor SDK.
Support server-side and client-side apps
To support Razor component consumption by server-side and client-side apps from a single library, use the following instructions for your editor.
Use the Razor Class Library project template.
Note
Do not select the Support pages and views checkbox. Selecting the checkbox results in a class library that only supports server-side apps.
The library generated from the project template:
- Targets the current .NET framework based on the installed SDK.
- Enables browser compatibility checks for platform dependencies by including
browseras a supported platform with theSupportedPlatformMSBuild item. - Adds a NuGet package reference for Microsoft.AspNetCore.Components.Web.
RazorClassLibrary-CSharp.csproj (reference source)
Note
Documentation links to .NET reference source usually load the repository's default branch, which represents the current development for the next release of .NET. To select a tag for a specific release, use the Switch branches or tags dropdown list.
Support multiple framework versions
If the library must support features added to Blazor in the current release while also supporting one or more earlier releases, multi-target the library. Provide a semicolon-separated list of Target Framework Monikers (TFMs) in the TargetFrameworks MSBuild property:
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
The preceding example targets .NET 10 and .NET 11.
Only support server-side consumption
Class libraries are rarely built to only support server-side apps. If the class library only requires server-side-specific features, such as access to CircuitHandler or Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage, or uses ASP.NET Core-specific features, such as middleware, MVC controllers, or Razor Pages, use one of the following approaches:
Specify that the library supports pages and views when the library is created with the Support pages and views checkbox (Visual Studio) or the
-s|--support-pages-and-viewsoption with thedotnet newcommand:dotnet new razorclasslib -sOnly provide a framework reference to ASP.NET Core in the library's project file in addition to any other required MSBuild properties:
<ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> </ItemGroup>
For more information on libraries containing Razor components, see Consume ASP.NET Core Razor components from a Razor class library (RCL).
Include MVC extensibility
This section outlines recommendations for libraries that include:
For guidance on multi-targeting, see Support multiple framework versions.
Razor views or Razor Pages
A project that includes Razor views or Razor Pages must use the Microsoft.NET.Sdk.Razor SDK.
The project requires:
- An
AddRazorSupportForMvcMSBuild property set totrue. - A
<FrameworkReference>element for the shared framework.
The Razor Class Library project template satisfies the preceding requirements for ASP.NET Core projects. Use the following instructions for your editor.
Use the Razor Class Library project template. The template's Support pages and views checkbox should be selected.
For example:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
Tag Helpers
A project that includes Tag Helpers should use the Microsoft.NET.Sdk SDK. Add a <FrameworkReference> element for the shared framework. For example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
View components
A project that includes View components should use the Microsoft.NET.Sdk SDK. Add a <FrameworkReference> element for the shared framework. For example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
Use an API removed from the shared framework
To use an ASP.NET Core assembly that was removed from the shared framework, add the appropriate package reference.
For example, to add the web API client:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
</ItemGroup>
</Project>
Related content
ASP.NET Core