Blazor, a powerful framework from Microsoft, enables coders to build and deploy interactive web applications using C# and .NET instead of JavaScript. It supports two primary hosting models as Blazor Server and Blazor WebAssembly (WASM). Both provide rich user experiences but differ significantly in architecture and use case.
What is Blazor Server and How It Works?
- The app runs on the server.
- The UI gets rendered on the server and updates are sent via SignalR connection (a real-time WebSocket-based communication) to the browser.
- When a user interacts (e.g., button click), the event is sent to the server, processed, and the updated UI is sent back.
Architecture:
Browser <-- SignalR --> Server (.NET Runtime)
Pros:
- Fast initial load – no big DLLs are downloaded.
- Full access to server-side resources (e.g., database, files).
- Code stays on the server (more secure).
Cons:
- Requires a constant internet connection.
- Scalability might be limited due to maintaining many concurrent connections.
- Higher latency for UI interactions.
Example:
@page "/counter"
<h3>Counter</h3><p>Current count: @count</p><button class="btn btn-primary" @onclick="IncrementCount">Click me</button>@code { private int count = 0; private void IncrementCount() { count++; }
}
This component runs on the server. When the button is clicked, the server processes the logic and updates the UI via SignalR.
Blazor WebAssembly (WASM) – How It Works:
- The app runs completely in the browser using WebAssembly.
- The entire .NET runtime and app DLLs are downloaded to the client’s browser.
- Once loaded, everything runs client-side, without constant server communication.
Architecture:
Browser (.NET Runtime running via WebAssembly)
Pros:
- Runs offline once loaded.
- No server round-trips for UI logic – very fast interactions.
- More scalable (no SignalR connections to maintain).
Cons:
- Larger download size (can slow initial page load).
- Limited access to server resources (must use APIs for DB, etc.).
- Runs in a sandboxed environment (some features restricted).
Example (same UI logic as Blazor Server):
@page "/counter"
<h3>Counter</h3><p>Current count: @count</p><button class="btn btn-primary" @onclick="IncrementCount">Click me</button>@code { private int count = 0; private void IncrementCount() { count++; }
}
The only difference is where the code executes in this case, inside the user’s browser.
Blazor Server vs Blazor WebAssembly
Feature | Blazor Server | Blazor WebAssembly |
Hosting Requirement | ASP.NET Core server | Static site possible (e.g., GitHub Pages, Azure Blob) |
Load Speed | Faster initial load | Slower first load, faster after |
Offline Support | ❌ No | ✅ Yes |
Access to Database | ✅ Direct access (EF Core, etc.) | ❌ Needs API backend |
Best for | Intranet apps, admin panels | Public-facing apps (e.g., blogs) |
Conclusion
Blazor gives developers flexibility in choosing how their app is delivered and executed. Blazor Server is ideal for apps needing real-time server interactions and tight control over data, while Blazor WebAssembly shines in client-heavy, scalable, and offline-ready applications.
Choosing the right hosting model depends on your app’s performance, scalability, and infrastructure needs but with Blazor, you’re empowered to build rich UIs all with .NET.