Sunday 13 October 2019

ASP.NET Core Razor components class libraries

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

Follow the guidance in the Get started with ASP.NET Core Blazor article to configure your environment for Blazor.
  • In 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 MyComponentLib1. 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.
In .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 MyComponentLib1. The folder that holds MyComponentLib1 is created automatically when the command is executed:
    .NET Core CLI
    dotnet new razorclasslib -o MyComponentLib1
    
  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:
    .NET Core CLI
    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, MyComponentLib1 is a component library containing a SalesReport component.
The SalesReport component can be referenced using its full type name with namespace:
CSHTML
<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:
CSHTML
@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 in a command shell:
.NET Core CLI
dotnet pack
Upload the package to NuGet using the dotnet nuget push command in a command shell.

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.

No comments:

Post a Comment