AspNetCore.Docs/aspnetcore/blazor/components/class-libraries.md

9.6 KiB

title author description monikerRange ms.author ms.custom ms.date no-loc uid
ASP.NET Core Razor components class libraries guardrex Discover how components can be included in Blazor apps from an external component library. >= aspnetcore-3.1 riande mvc 07/27/2020
ASP.NET Core Identity
cookie
Cookie
Blazor
Blazor Server
Blazor WebAssembly
Identity
Let's Encrypt
Razor
SignalR
blazor/components/class-libraries

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

Visual Studio

  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.

.NET Core CLI

  1. Use the Razor Class Library template (razorclasslib) with the 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:

    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:

    @using Microsoft.AspNetCore.Components.Web
    

    Manually add the file the root of the generated project.

  2. To add the library to an existing project, use the 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:

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

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

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

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

::: 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 xref:razor-pages/ui-class#create-an-rcl-with-static-assets.

Supply components and static assets to multiple hosted Blazor apps

For more information, see xref:blazor/host-and-deploy/webassembly#static-assets-and-class-libraries.

::: 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 xref:System.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 xref:System.Runtime.Versioning.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 xref:blazor/call-javascript-from-dotnet#blazor-javascript-isolation-and-object-references.

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

dotnet pack

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

Additional resources

::: moniker range=">= aspnetcore-5.0"

::: moniker-end

::: moniker range="< aspnetcore-5.0"

::: moniker-end