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

5.7 KiB

title author description monikerRange ms.author ms.custom ms.date uid
Razor components class libraries guardrex Discover how components can be included in Blazor apps from an external component library. >= aspnetcore-3.0 riande mvc 05/06/2019 blazor/class-libraries

Razor components class libraries

By Simon Timms

Components can be shared in Razor class libraries across projects. Components 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 Razor class libraries are normal .NET assemblies.

Create a Razor class library

Follow the guidance in the xref:blazor/get-started article to configure your environment for Blazor.

Visual Studio

  1. Create a new project.
  2. Select ASP.NET Core Web Application. Select Next.
  3. Provide a project name in the Project name field or accept the default project name. The examples in this topic use the project name MyComponentLib1. Select Create.
  4. In the Create a new ASP.NET Core Web Application dialog, confirm that .NET Core and ASP.NET Core 3.0 are selected.
  5. Select the Razor Class Library template. Select Create.
  6. Add the Razor class library to a solution:
    1. Right-click the solution. Select Add > Existing Project.
    2. Navigate to the Razor class library's project file.
    3. Select the Razor class library's project file (.csproj).
  7. Add a reference the Razor class library from the app:
    1. Right-click the app project. Select Add > Reference.
    2. Select the Razor class library project. Select OK.

Visual Studio Code / .NET Core CLI

  1. Use the Razor class library (razorclasslib) template with the dotnet new command from a command shell. In the following example, a Razor class library is created named MyComponentLib1. The folder that holds MyComponentLib1 is created automatically when the command is executed.

    dotnet new razorclasslib -o MyComponentLib1
    
  2. To add the library to an existing project, use the dotnet add reference command from a command shell. In the following example, the Razor class library 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}
    

Add Razor component files (.razor) to the Razor class library.

Razor class libraries not supported for client-side apps

In ASP.NET Core 3.0 Preview, Razor class libraries aren't compatible with Blazor client-side apps.

For Blazor client-side apps, use a Blazor component library created by the blazorlib template from a command shell:

dotnet new blazorlib -o MyComponentLib1

Component libraries using the blazorlib template can include static files, such as images, JavaScript, and stylesheets. At build time, static files are embedded into the built assembly file (.dll), which allows consumption of the components without having to worry about how to include their resources. Any files included in the content directory are marked as an embedded resource.

Static assets not supported for server-side apps

In ASP.NET Core 3.0 Preview, Blazor server-side apps can't consume static assets from either a Razor class library (razorclasslib) or a Blazor library (blazorlib).

As a temporary workaround, you can try BlazorEmbedLibrary.

[!NOTE] BlazorEmbedLibrary isn't maintained or supported by Microsoft.

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, MyComponentLib1 is a component library containing a Sales Report (SalesReport) component.

The Sales Report component can be referenced using its full type name with namespace:

<h1>Hello, world!</h1>

Welcome to your new app.

<MyComponentLib1.SalesReport />

The component can also be referenced if the library is brought into scope with an @using directive:

@using MyComponentLib1

<h1>Hello, world!</h1>

Welcome to your new app.

<SalesReport />

Include the @using MyComponentLib1 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 page or set of pages within a folder.

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

dotnet pack

Upload the package to NuGet using the dotnet nuget publish command from a command shell:

dotnet nuget publish

When using the blazorlib template, static resources are included in the NuGet package. Library consumers automatically receive scripts and stylesheets, so consumers aren't required to manually install the resources. Note that static assets aren't supported for server-side apps, including when a Blazor library (blazorlib) is referenced by a server-side app.