Blazor

Note: Blazor is a web framework for building interactive client-side web UIs with .NET. It enables developers to create rich, interactive web applications using C# and .NET, instead of JavaScript.

Introduction to Blazor

Blazor allows you to build reusable UI components using C# and Razor syntax. You can host these components in two primary ways:

Blazor Server

In Blazor Server, your .NET code runs on the server, and UI updates are sent to the browser over a SignalR connection. This model offers:

Example:

<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

Blazor WebAssembly

Blazor WebAssembly (WASM) runs your .NET code directly in the browser using the WebAssembly runtime. This provides a true client-side single-page application (SPA) experience:

Considerations:

Components

Blazor applications are built using reusable UI components. These components are typically defined in Razor files (`.razor`) which combine HTML markup and C# code.

Component Structure:

Routing

Blazor uses a file-based routing system. Components marked with the @page directive can be navigated to directly.

Example Route:

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

To navigate between pages, you can use the NavLink component:

<NavLink href="/" Match="NavLinkMatch.All">Home</NavLink>
<NavLink href="/counter">Counter</NavLink>

Data Binding

Blazor supports two-way data binding, simplifying synchronization between UI elements and component state.

Example Two-Way Binding:

<input type="text" @bind="Name" />
<p>Hello, @Name!</p>

@code {
    public string Name { get; set; } = "World";
}

Event Handling

Event handlers are methods that are invoked when a specific UI event occurs. Use the @oneventname directive.

Example:

<button @onclick="HandleClick">Click Me</button>

@code {
    private void HandleClick()
    {
        Console.WriteLine("Button clicked!");
    }
}

Dependency Injection

Blazor has built-in support for Dependency Injection (DI), making it easy to manage service lifetimes and inject dependencies into your components.

Registering Services (in Program.cs or Startup.cs):

builder.Services.AddScoped<IMyService, MyService>();

Injecting Services:

@inject IMyService MyService

<p>Service message: @MyService.GetMessage()</p>

@code {
    // ...
}

State Management

Managing state in Blazor applications can be achieved through various patterns:

Authentication and Authorization

Blazor provides robust mechanisms for handling authentication and authorization, similar to ASP.NET Core MVC and Razor Pages.

<AuthorizeView>
    <Authorized>
        <p>Welcome, @context.User.Identity.Name!</p>
    </Authorized>
    <NotAuthorized>
        <p>Please log in.</p>
    </NotAuthorized>
</AuthorizeView>

Deployment

Deploying Blazor applications depends on the hosting model:

Considerations for Blazor WASM deployment include: