ASP.NET Core Razor components class libraries

By Simon Timms

Components can be shared in a Razor class library (RCL) 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

  1. Create a new project.
  2. Select Razor Class Library. Select Next.
  3. In the Create a new Razor class library dialog, select Create.
  4. 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.
  5. Add the RCL to a solution:
    1. Right-click the solution. Select Add > Existing Project.
    2. Navigate to the RCL's project file.
    3. Select the RCL's project file (.csproj).
  6. Add a reference the RCL from the app:
    1. Right-click the app project. Select Add > Reference.
    2. 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:

@using Microsoft.AspNetCore.Components.Web

Manually add the file the root of the generated project.

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 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:

<h1>Hello, world!</h1>

Welcome to your new app.

<ComponentLibrary.Component1 />

Alternatively, bring the library into scope with an @using directive and use the component without its namespace:

@using ComponentLibrary

<h1>Hello, world!</h1>

Welcome to your new app.

<Component1 />

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.

To provide Component1's my-component CSS class to the component, link to the library's stylesheet using the framework's Link component in Component1.razor:

<div class="my-component">
    <Link href="_content/ComponentLibrary/styles.css" rel="stylesheet" />

    <p>
        This Blazor component is defined in the <strong>ComponentLibrary</strong> package.
    </p>
</div>

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):

<head>
    ...
    <link href="_content/ComponentLibrary/styles.css" rel="stylesheet" />
</head>

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 <link> 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 <link> HTML tag can't be modified by application state.
  • Is removed from the HTML <head> when the parent component is no longer rendered.

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):

<head>
    ...
    <link href="_content/ComponentLibrary/styles.css" rel="stylesheet" />
</head>

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 Reusable Razor UI in class libraries with ASP.NET Core.

Supply components and static assets to multiple hosted Blazor apps

For more information, see Host and deploy ASP.NET Core Blazor WebAssembly.

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 PlatformNotSupportedException 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:

<ItemGroup>
  <SupportedPlatform Include="browser" />
</ItemGroup>

When authoring a library, indicate that a particular API isn't supported in browsers by specifying browser to UnsupportedOSPlatformAttribute:

[UnsupportedOSPlatform("browser")]
private static string GetLoggingDirectory()
{
    ...
}

For more information, see Annotating APIs as unsupported on specific platforms (dotnet/designs GitHub repository.

Blazor JavaScript isolation and object references

Blazor enables JavaScript isolation in standard JavaScript 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 Call JavaScript functions from .NET methods in ASP.NET Core Blazor.

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 command in a command shell:

dotnet pack

Upload the package to NuGet using the dotnet nuget push command in a command shell.

Additional resources