---
title: ASP.NET Core Razor components class libraries
author: guardrex
description: Discover how components can be included in Blazor apps from an external component library.
monikerRange: '>= aspnetcore-3.1'
ms.author: riande
ms.custom: mvc
ms.date: 07/27/2020
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
uid: blazor/components/class-libraries
---
# ASP.NET Core Razor components class libraries
By [Simon Timms](https://github.com/stimms)
Components can be shared in a [Razor class library (RCL)](xref:razor-pages/ui-class) across projects. A *Razor components class library* can be included from:
* Another project in the solution.
* A NuGet package.
* A referenced .NET library.
Just as components are regular .NET types, components provided by an RCL are normal .NET assemblies.
## Create an RCL
# [Visual Studio](#tab/visual-studio)
1. Create a new project.
1. Select **Razor Class Library**. Select **Next**.
1. In the **Create a new Razor class library** dialog, select **Create**.
1. Provide a project name in the **Project name** field or accept the default project name. The examples in this topic use the project name `ComponentLibrary`. Select **Create**.
1. Add the RCL to a solution:
1. Right-click the solution. Select **Add** > **Existing Project**.
1. Navigate to the RCL's project file.
1. Select the RCL's project file (`.csproj`).
1. Add a reference the RCL from the app:
1. Right-click the app project. Select **Add** > **Reference**.
1. Select the RCL project. Select **OK**.
> [!NOTE]
> If the **Support pages and views** check box is selected when generating the RCL from the template, then also add an `_Imports.razor` file to root of the generated project with the following contents to enable Razor component authoring:
>
> ```razor
> @using Microsoft.AspNetCore.Components.Web
> ```
>
> Manually add the file the root of the generated project.
# [.NET Core CLI](#tab/netcore-cli)
1. Use the **Razor Class Library** template (`razorclasslib`) with the [`dotnet new`](/dotnet/core/tools/dotnet-new) command in a command shell. In the following example, an RCL is created named `ComponentLibrary`. The folder that holds `ComponentLibrary` is created automatically when the command is executed:
```dotnetcli
dotnet new razorclasslib -o ComponentLibrary
```
> [!NOTE]
> If the `-s|--support-pages-and-views` switch is used when generating the RCL from the template, then also add an `_Imports.razor` file to root of the generated project with the following contents to enable Razor component authoring:
>
> ```razor
> @using Microsoft.AspNetCore.Components.Web
> ```
>
> Manually add the file the root of the generated project.
1. To add the library to an existing project, use the [`dotnet add reference`](/dotnet/core/tools/dotnet-add-reference) command in a command shell. In the following example, the RCL is added to the app. Execute the following command from the app's project folder with the path to the library:
```dotnetcli
dotnet add reference {PATH TO LIBRARY}
```
---
## Consume a library component
In order to consume components defined in a library in another project, use either of the following approaches:
* Use the full type name with the namespace.
* Use Razor's [`@using`](xref:mvc/views/razor#using) directive. Individual components can be added by name.
In the following examples, `ComponentLibrary` is a component library containing the `Component1` component (`Component1.razor`). The `Component1` component is an example component automatically added by the RCL project template when the library is created.
Reference the `Component1` component using its namespace:
```razor
Hello, world!
Welcome to your new app.
```
Alternatively, bring the library into scope with an [`@using`](xref:mvc/views/razor#using) directive and use the component without its namespace:
```razor
@using ComponentLibrary
Hello, world!
Welcome to your new app.
```
Optionally, include the `@using ComponentLibrary` directive in the top-level `_Import.razor` file to make the library's components available to an entire project. Add the directive to an `_Import.razor` file at any level to apply the namespace to a single component or set of components within a folder.
::: moniker range=">= aspnetcore-5.0"
To provide `Component1`'s `my-component` CSS class to the component, link to the library's stylesheet using the framework's [`Link` component](xref:blazor/fundamentals/additional-scenarios#influence-html-head-tag-elements) in `Component1.razor`:
```razor
This Blazor component is defined in the ComponentLibrary package.
```
To provide the stylesheet across the app, you can alternatively link to the library's stylesheet in the app's `wwwroot/index.html` file (Blazor WebAssembly) or `Pages/_Host.cshtml` file (Blazor Server):
```html
...
```
When the `Link` component is used in a child component, the linked asset is also available to any other child component of the parent component as long as the child with the `Link` component is rendered. The distinction between using the `Link` component in a child component and placing a `` HTML tag in `wwwroot/index.html` or `Pages/_Host.cshtml` is that a framework component's rendered HTML tag:
* Can be modified by application state. A hard-coded `` HTML tag can't be modified by application state.
* Is removed from the HTML `` when the parent component is no longer rendered.
::: moniker-end
::: moniker range="< aspnetcore-5.0"
To provide `Component1`'s `my-component` CSS class, link to the library's stylesheet in the app's `wwwroot/index.html` file (Blazor WebAssembly) or `Pages/_Host.cshtml` file (Blazor Server):
```html
...
```
::: moniker-end
## Create a Razor components class library with static assets
An RCL can include static assets. The static assets are available to any app that consumes the library. For more information, see .
## Supply components and static assets to multiple hosted Blazor apps
For more information, see .
::: moniker range=">= aspnetcore-5.0"
## Browser compatibility analyzer for Blazor WebAssembly
Blazor WebAssembly apps target the full .NET API surface area, but not all .NET APIs are supported on WebAssembly due to browser sandbox constraints. Unsupported APIs throw when running on WebAssembly. A platform compatibility analyzer warns the developer when the app uses APIs that aren't supported by the app's target platforms. For Blazor WebAssembly apps, this means checking that APIs are supported in browsers. Annotating .NET framework APIs for the compatibility analyzer is an on-going process, so not all .NET framework API is currently annotated.
Blazor WebAssembly and Razor class library projects *automatically* enable browser compatibilty checks by adding `browser` as a supported platform with the `SupportedPlatform` MSBuild item. Library developers can manually add the `SupportedPlatform` item to a library's project file to enable the feature:
```xml
```
When authoring a library, indicate that a particular API isn't supported in browsers by specifying `browser` to :
```csharp
[UnsupportedOSPlatform("browser")]
private static string GetLoggingDirectory()
{
...
}
```
For more information, see [Annotating APIs as unsupported on specific platforms (dotnet/designs GitHub repository](https://github.com/dotnet/designs/blob/main/accepted/2020/platform-exclusion/platform-exclusion.md#build-configuration-for-platforms).
## Blazor JavaScript isolation and object references
Blazor enables JavaScript isolation in standard [JavaScript modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules). JavaScript isolation provides the following benefits:
* Imported JavaScript no longer pollutes the global namespace.
* Consumers of the library and components aren't required to manually import the related JavaScript.
For more information, see .
::: moniker-end
## Build, pack, and ship to NuGet
Because component libraries are standard .NET libraries, packaging and shipping them to NuGet is no different from packaging and shipping any library to NuGet. Packaging is performed using the [`dotnet pack`](/dotnet/core/tools/dotnet-pack) command in a command shell:
```dotnetcli
dotnet pack
```
Upload the package to NuGet using the [`dotnet nuget push`](/dotnet/core/tools/dotnet-nuget-push) command in a command shell.
## Additional resources
::: moniker range=">= aspnetcore-5.0"
*
* [Add an XML Intermediate Language (IL) Trimmer configuration file to a library](xref:blazor/host-and-deploy/configure-trimmer)
::: moniker-end
::: moniker range="< aspnetcore-5.0"
*
* [Add an XML Intermediate Language (IL) Linker configuration file to a library](xref:blazor/host-and-deploy/configure-linker#add-an-xml-linker-configuration-file-to-a-library)
::: moniker-end