Getting Started with Blazor Web App in .NET CLI

16 Feb 20244 minutes to read

This article provides a step-by-step instructions for building Blazor Web App with Syncfusion Blazor component using the .NET CLI.

Prerequisites

Latest version of the .NET Core SDK. If you previously installed the SDK, you can determine the installed version by executing the following command in a command prompt (Windows) or terminal (macOS) or command shell (Linux).

dotnet --version

Create a Blazor Web project using .NET CLI

Run the following command to create a new Blazor Web App in a command prompt (Windows) or terminal (macOS) or command shell (Linux).

For a Blazor Web App with Server interactive render mode & Per page/component interactivity location, use the following commands

dotnet new blazor -o BlazorApp
cd BlazorApp

For a Blazor Web App with WebAssembly interactive render mode & Per page/component interactivity location, use the following commands

dotnet new blazor -o BlazorApp -int WebAssembly
cd BlazorApp
cd BlazorApp.Client

For a Blazor Web App with Auto interactive render mode & Per page/component interactivity location, use the following commands

dotnet new blazor -o BlazorApp -int Auto
cd BlazorApp
cd BlazorApp.Client

This command creates new Blazor Web app project and places it in a new directory called BlazorApp inside your current location. See Create Blazor app topic and dotnet new CLI command topics for more details.

Install Syncfusion Blazor Calendars and Themes NuGet in the App

Here’s an example of how to add Blazor Calendar component in the application using the following command in the command prompt (Windows) or terminal (Linux and macOS) to install a Syncfusion.Blazor.Calendars and Syncfusion.Blazor.Themes NuGet package. See Install and manage packages using the dotnet CLI topics for more details.

If you utilize WebAssembly or Auto render modes in the Blazor Web App need to be install Syncfusion Blazor components NuGet packages within the client project.

dotnet add package Syncfusion.Blazor.Calendars --version 24.2.3
dotnet add package Syncfusion.Blazor.Themes --version 24.2.3
dotnet restore

NOTE

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

Register Syncfusion Blazor Service

Open ~/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.Calendars namespace.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Calendars

Now, register the Syncfusion Blazor Service in the ~/Program.cs file of your 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();
....

Add stylesheet and script resources

The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Include the stylesheet reference in the <head> section and the script reference at the end of the <body> in the ~/Components/App.razor file as shown below:

<head>
    ....
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.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.

Add Syncfusion Blazor component

Add the Syncfusion Blazor Calendar component in the ~/Pages/.razor file. If an interactivity location as Per page/component in the web app, define a render mode at the top of the ~Pages/.razor component, as follows:

@* desired render mode define here *@
@rendermode InteractiveAuto
<SfCalendar TValue="DateTime" />
  • In the command prompt (Windows) or terminal (Linux and macOS) to run the following command to build and start the app. The app listening on http://localhost:<port number> and view it in the browser.

  • If an interactive render mode as WebAssembly or Auto in the web app, you should run the Blazor server project.

dotnet run

Blazor Calendar Component

See Also