Getting Started with Blazor Barcode Generator Component in Web App
13 Aug 20268 minutes to read
This section briefly explains how to include the Blazor Barcode Generator component in a Blazor Web App using Visual Studio, Visual Studio Code, and the .NET CLI.
NOTE
The Blazor Barcode component requires .NET 8.0 or later and a modern browser with ES6 and WebAssembly support. The
Syncfusion.Blazor.BarcodeGeneratorpackage is compatible with theServer,WebAssembly, andAutointeractive render modes.
Create a new Blazor Web App
Prerequisites
- System requirements for Blazor components
- Install the Syncfusion® Blazor Extension for Visual Studio if you plan to use the Syncfusion templates to create the project.
Create a Blazor Web App using Visual Studio via Microsoft Templates or the Blazor Extension.
NOTE
Configure the appropriate Interactive render mode and Interactivity location while creating the project.
If you use the Syncfusion® Blazor Extension, a sample application can be generated automatically.
Prerequisites
- System requirements for Blazor components
- Install the Syncfusion® Blazor Extension for Visual Studio Code if you plan to use the Syncfusion templates to create the project.
Run the following command to create a new Blazor Web App.
dotnet new blazor -o BlazorWebApp --interactivity Auto -f net8.0
cd BlazorWebApp
cd BlazorWebApp.ClientAlternatively, create a Blazor Web App using Visual Studio Code via Microsoft Templates, the Blazor Extension, or the C# Dev Kit extension.
NOTE
Configure the appropriate Interactive render mode and Interactivity location while creating the project.
If you use the Syncfusion® Blazor Extension, a sample application can be generated automatically.
Prerequisites
Install the latest version of .NET SDK. If you previously installed the SDK, determine the installed version by executing the following command in a command prompt (Windows) or terminal (macOS) or command shell (Linux).
dotnet --versionRun the following command to create a new Blazor Web App.
dotnet new blazor -o BlazorWebApp --interactivity Auto -f net8.0
cd BlazorWebApp
cd BlazorWebApp.ClientNOTE
See the dotnet new CLI command and interactive render mode documentation topics for more details.
NOTE
Configure the appropriate Interactive render mode and Interactivity location while creating a Blazor Web App. For detailed information, refer to the interactive render mode documentation.
Install the required Blazor packages
Install the Syncfusion.Blazor.BarcodeGenerator NuGet packages. All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details. If using the WebAssembly or Auto render modes in the Blazor Web App, install these packages in the .Client project.
- Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
- Search the required NuGet packages (
Syncfusion.Blazor.BarcodeGenerator) and install them.
Alternatively, you can install the same packages using the Package Manager Console with the following commands.
Install-Package Syncfusion.Blazor.BarcodeGenerator -Version 34.2.2Open the terminal and run the following commands.
dotnet add package Syncfusion.Blazor.BarcodeGenerator -v 34.2.2Open the command prompt and run the following commands.
dotnet add package Syncfusion.Blazor.BarcodeGenerator -v 34.2.2NOTE
All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details.
Import namespaces
After the packages are installed, open the ~/_Imports.razor file in the .Client project and import the Syncfusion.Blazor and Syncfusion.Blazor.BarcodeGenerator namespaces.
@using Syncfusion.Blazor
@using Syncfusion.Blazor.BarcodeGeneratorRegister the Blazor service
Open the Program.cs file in Blazor Web App and register the Blazor service. If the Interactive Render Mode is set to WebAssembly or Auto, register the Blazor service in Program.cs files of both the server and client projects in your Blazor Web App.
....
using Syncfusion.Blazor;
....
builder.Services.AddSyncfusionBlazor();
....NOTE
If the Interactive Render Mode is set to
WebAssemblyorAuto, register the Blazor service in Program.cs files of both the server and client projects in your Blazor Web App.
NOTE
Register your Syncfusion license key before calling
AddSyncfusionBlazor()by usingSyncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR_LICENSE_KEY"). A missing or invalid license will cause trial license warnings at runtime. See the Syncfusion Licensing FAQ for details.
Add Script Resources
Include the script references in the ~/Components/App.razor file.
<body>
....
<script src="_content/Syncfusion.Blazor.BarcodeGenerator/scripts/sf-barcode.min.js" type="text/javascript"></script>
</body>Add Blazor Barcode Generator component
Open a Razor file located in the ~/Pages/*.razor (for example, Home.razor) and add the Blazor Barcode Generator component inside the .Client project razor file.
NOTE
If the interactivity location is set to
Per page/componentin the Web App, define a render mode at the top of the razor file. (For example,InteractiveServer,InteractiveWebAssembly, orInteractiveAuto). If the Interactivity is set toGlobalwithAutoorWebAssembly, the render mode is automatically configured in theApp.razorfile by default.
@rendermode InteractiveAuto
<SfBarcodeGenerator Width="200px" Height="150px" Type="@BarcodeType.Codabar" Value="123456789"></SfBarcodeGenerator>NOTE
The following table shows which render mode to choose for each hosting model:
| Hosting model | Render mode | Use case |
|---|---|---|
| Server | @rendermode InteractiveServer |
Server-side rendering with SignalR. |
| WebAssembly | @rendermode InteractiveWebAssembly |
Client-side rendering, no server required after first load. |
| Auto | @rendermode InteractiveAuto |
WebAssembly on first visit, then Server for subsequent visits. |
Run the application
Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application. The Blazor Barcode Generator component will render in your default web browser.
Open the terminal and navigate to the main project folder (for example, BlazorWebApp) and run the following command.
cd ..
cd BlazorWebApp
dotnet runOpen the command prompt and navigate to the main project folder (for example, BlazorWebApp) and run the following command.
cd ..
cd BlazorWebApp
dotnet run
NOTE
Add the QR Code Generator component
Add a QR code to the Barcode Generator component.
<SfQRCodeGenerator Width="200px" Height="150px" Value="Syncfusion"></SfQRCodeGenerator>
Add the Data Matrix Generator component
Add a Data Matrix code to the Barcode Generator component.
<SfDataMatrixGenerator Width="200px" Height="150px" Value="SYNCFUSION"></SfDataMatrixGenerator>