MSDN Documentation

Understanding Blazor Hosting Models

This tutorial explores the different hosting models available for Blazor applications, allowing you to choose the most suitable architecture for your project.

Introduction

Blazor is a web framework for building interactive client-side web UIs with .NET. It enables developers to build modern web applications that leverage C# and Razor syntax, rather than JavaScript. A key aspect of Blazor's flexibility is its support for different hosting models, which dictate where the .NET code runs and how it interacts with the browser.

1. Blazor Server

In the Blazor Server hosting model, your .NET code runs on the server. A real-time connection is established between the browser and the server using SignalR. UI updates are sent over this connection from the server to the browser.

When to use: Applications requiring access to server-side resources, or when a rich, interactive UI is needed with minimal client-side complexity.

2. Blazor WebAssembly (Wasm)

Blazor WebAssembly allows you to host your Blazor application entirely in the browser. The .NET runtime and your application's assemblies are downloaded to the client, and the code executes directly within the browser's WebAssembly environment.

Key Feature: Runs C# code directly in the browser using WebAssembly.

When to use: Single Page Applications (SPAs), offline-capable applications, or when you want to maximize client-side performance and reduce server costs.

3. Blazor Hybrid

Blazor Hybrid combines the best of both worlds. It allows you to host Blazor components within native desktop or mobile applications. The .NET code runs natively on the device, while the UI is rendered in a web view control.

When to use: Building cross-platform native applications using Blazor components, or when migrating existing desktop applications to use web UI technologies.

Note: Blazor Web App is a new unified model introduced in .NET 8, combining Blazor Server and Blazor WebAssembly into a single project type that can dynamically switch between them or even use both.

Choosing the Right Model

The choice of hosting model depends on your application's requirements:

Conclusion

Understanding Blazor's hosting models is crucial for architecting efficient and performant web applications. By selecting the appropriate model, you can optimize for user experience, server resource utilization, and application complexity.