Creating Razor Class Library (RCL) using Syncfusion Blazor components

18 Dec 20235 minutes to read

This section provides information about creating Razor Class Library with the Syncfusion Blazor components using Visual Studio.

Prerequisites

Create a Razor Class Library in Visual Studio 2022

  1. Choose Create a new project from the Visual Studio dashboard.

    new project in aspnetcore blazor

  2. Select Razor Class Library from the template, and then click the Next button.

    razor class template

  3. Now, the project configuration window will popup. Click Create button to create a new project with the default project configuration.

    razor class project configuration

  4. Select the target Framework .NET 8 at the top of the Application based on your required target that you want and then click the Create button to create a new Razor Class Library application.

    select framework

Install Syncfusion Blazor Calendars and Themes NuGet in the App

To add Blazor Calendar component in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search and install Syncfusion.Blazor.Calendars and Syncfusion.Blazor.Themes. Alternatively, you can utilize the following package manager command to achieve the same.

Install-Package Syncfusion.Blazor.Calendars -Version 24.2.3

Install-Package Syncfusion.Blazor.Themes -Version 24.2.3

NOTE

Syncfusion Blazor components are available in nuget.org. Refer to NuGet packages topic for available NuGet packages list with component details.

Importing Syncfusion Blazor component in Razor Class Library

Now, import and add the Syncfusion Blazor components in the ~/Component1.razor file. For example, the Calendar component is imported and added in the ~/Component1.razor page.

@using Syncfusion.Blazor.Calendars

<div class="my-component">
This Blazor component is defined in the <strong>RazorClassLibrary</strong> package.
</div><br />

<SfCalendar TValue="DateTime"></SfCalendar>

Create a Blazor project in Visual Studio

Configure the Razor Class Library and Blazor Application

  1. Now, Right-click the solution, and then select Add/Existing Project.

    razor class library in blazor app

  2. Add the Razor Class Library project by selecting the RazorClassLibrary.csproj file.

    add RCL in blazor app

    NOTE

    Razor Class Library project is added to the existing Blazor Application.

  3. Right-click the Blazor App project, and then select Add/Project reference. Now, click the checkbox and configure the Razor Class Library.

    reference manager in blazor app

    select RCL to configure blazor app

Importing Razor Class Library in the Blazor Application

  1. Open ~/_Imports.razor file in Blazor App and import the RazorClassLibrary.

     @using RazorClassLibrary
  2. Now, register the Syncfusion Blazor Service in the ~/Program.cs file of your Blazor App.

    • If you select an Interactive render mode as WebAssembly or Auto, you need to register the Syncfusion Blazor service in both ~/Program.cs files of your Blazor Web App.
     ....
     using Syncfusion.Blazor;
     ....
     builder.Services.AddSyncfusionBlazor();
     ....
  3. For Blazor Web App, include the stylesheet reference in the <head> section and the script reference at the end of the <body> in the ~/Components/App.razor file:

    • For Blazor WebAssembly app, include the stylesheet reference in the <head> section and the script reference at the end of the <body> in the ~/wwwroot/index.html file.

    • For .NET 7 project in Blazor Server App, include the stylesheet reference in the <head> section and the script reference at the end of the <body> in the ~/Pages/_Host.cshtml file.

    • For .NET 6 project in Blazor Server App, include the stylesheet reference in the <head> section and the script reference at the end of the <body> in the ~/Pages/_Layout.cshtml file.

     <head>
         ....
         <link href="_content/Syncfusion.Blazor.Themes/bootstrap4.css" rel="stylesheet" />
     </head>
     ....
     <body>
         ....
         <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
     </body>

    NOTE

    Check out the Blazor Themes topic to discover various methods (Static Web Assets, CDN, and CRG) for referencing themes in your Blazor application. Also, check out the Adding Script Reference topic to learn different approaches for adding script references in your Blazor application.

  4. Now, add the created custom component in the ~/Pages/.razor file.

     <Component1></Component1>
  5. Run the application, The Syncfusion Blazor Calendar component will be rendered in the default web browser.

    RCL output