Creating Razor Class Library (RCL) using Syncfusion® Blazor components
29 Nov 20245 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
-
Choose Create a new project from the Visual Studio dashboard.
-
Select Razor Class Library from the template, and then click the Next button.
-
Now, the project configuration window will popup. Click Create button to create a new project with the default project configuration.
-
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.
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 28.1.33
Install-Package Syncfusion.Blazor.Themes -Version 28.1.33
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
-
Refer to the Blazor Tooling documentation to create a new Blazor Web App using Visual Studio.
-
Refer to the Blazor Tooling documentation to create a new Blazor Server App or Blazor WebAssembly App using Visual Studio.
Configure the Razor Class Library and Blazor Application
-
Now, Right-click the solution, and then select Add/Existing Project.
-
Add the Razor Class Library project by selecting the
RazorClassLibrary.csproj
file.NOTE
Razor Class Library project is added to the existing Blazor Application.
-
Right-click the Blazor App project, and then select Add/Project reference. Now, click the checkbox and configure the Razor Class Library.
Importing Razor Class Library in the Blazor Application
-
Open ~/_Imports.razor file in Blazor App and import the
RazorClassLibrary
.@using RazorClassLibrary
-
Now, register the Syncfusion® Blazor Service in the ~/Program.cs file of your Blazor App.
- If you select an Interactive render mode as
WebAssembly
orAuto
, you need to register the Syncfusion® Blazor service in both ~/Program.cs files of your Blazor Web App.
.... using Syncfusion.Blazor; .... builder.Services.AddSyncfusionBlazor(); ....
- If you select an Interactive render mode as
-
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.
-
-
Now, add the created custom component in the ~/Pages/.razor file.
<Component1></Component1>
-
Run the application, The Syncfusion® Blazor Calendar component will be rendered in the default web browser.