Skip to content

9.9 — Blazor Overview: Server, WebAssembly and Auto

← Roadmap · Section: 9. MVC, Razor Pages and Views Prev: 9.8 ViewModels · Next: 9.10 Front-end integration

Markers: ⭐ · Confidence: 🔴 · Last reviewed: —


TL;DR

Blazor lets you build interactive web UI in C# instead of JavaScript, using reusable components.

  • Blazor Server — components run on the server, UI updates travel over a SignalR WebSocket. Small download, needs a constant connection.
  • Blazor WebAssembly — components run in the browser on a .NET runtime compiled to WASM. Large first download, then works offline.
  • Blazor Web App (.NET 8+) — one project that can use both, choosing a render mode per component. This is the modern default.

Definitions

Term What it is What it means
Blazor a UI framework Build interactive web UI with C# components instead of JavaScript.
Component a .razor file A reusable piece of UI with its own markup, state and logic.
Render mode a per-component setting (.NET 8+) Decides where and how a component runs.
Blazor Server a render mode Runs on the server; UI diffs sent over SignalR.
Blazor WebAssembly (WASM) a render mode Runs in the browser on a WASM .NET runtime.
Blazor Web App a project template (.NET 8+) Unifies both, with per-component render modes.
Static SSR a render mode (.NET 8+) Rendered server-side as plain HTML. No interactivity.
Auto a render mode (.NET 8+) Server first, then WASM once the runtime has downloaded.
Circuit a Blazor Server concept The server-side connection and state for one user's session.
Prerendering a technique Rendering the initial HTML server-side so the page appears before interactivity is ready.
@rendermode a directive (.NET 8+) Sets a component's render mode.
JS interop a mechanism Calling JavaScript from C# and vice versa.

The concept

What Blazor is for

Traditional server-rendered pages (9.1) reload on every interaction. Anything richer meant JavaScript — and for a .NET team, a second language, a second toolchain and a second set of skills.

Blazor lets you write that interactive UI in C#, sharing models and validation logic with the server.

@* A Blazor component *@
<h3>Counter</h3>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount() => currentCount++;
}

Clicking the button runs C#, updates state, and the UI re-renders — with no page reload and no JavaScript written by you.

⚠️ The honest framing for an interview

Blazor is worth knowing about rather than knowing deeply, unless the role is specifically Blazor.

It is genuinely good for internal tools, line-of-business applications and teams with deep .NET skills and no front-end specialists. It is a harder sell against React or Vue for a public-facing consumer site, where the ecosystem, hiring pool and bundle sizes still favour JavaScript.

Being able to explain the render modes and their trade-offs is the realistic interview expectation. Claiming deep Blazor experience you do not have will not survive one follow-up.


How it works

The simple version — Blazor Server

The component runs on the server. The browser holds a thin JavaScript runtime connected over a SignalR WebSocket.

flowchart LR
    B["Browser<br/>thin JS client"] -->|"click event"| S["Server<br/>component runs here"]
    S -->|"UI diff"| B
  1. You click a button.
  2. The event is sent over the WebSocket.
  3. The component's C# runs on the server, updating its state.
  4. The server computes what changed and sends just the diff.
  5. The browser patches the DOM.
Blazor Server
Initial download Small — a few KB of JS
Startup Fast
Works offline ❌ No
Latency sensitive ⚠️ Yes — every interaction is a round trip
Server memory ⚠️ Per connected user
Code on the client ❌ Stays on the server
Database access ✅ Direct

⚠️ The two real problems are latency and scale. Every keystroke in a form with an @oninput handler is a network round trip — on a 200 ms connection, typing feels broken. And the server holds a circuit in memory per connected user, so a thousand concurrent users means a thousand live component trees plus their state.

⚠️ A dropped connection loses the UI state. Blazor shows a reconnection overlay and tries to resume, but the circuit may already be gone — on mobile networks that is a frequent, visible failure.

Blazor WebAssembly

The component runs in the browser, on a .NET runtime compiled to WebAssembly.

Blazor WebAssembly
Initial download ⚠️ Large — several MB for the runtime
Startup ⚠️ Slow first load
Works offline ✅ Yes — can be a PWA
Latency sensitive ✅ No — runs locally
Server memory ✅ None per user
Code on the client ⚠️ Downloaded — assume it is readable
Database access ❌ Must call an API

⚠️ Your code is downloaded to the browser. DLLs can be inspected and decompiled (3.2), so no secrets, no connection strings, and no security decisions client-side. Every authorization check must be repeated on the server — client-side checks are a UX convenience only.

⚠️ And it cannot touch a database. It has to call an API, which means building and securing one.

.NET 8's Blazor Web App — render modes per component

The interesting development. One project, and each component declares how it runs:

@* Static — plain server-rendered HTML, no interactivity. Fast, cheap. *@
<ProductList />

@* Server — interactive over SignalR *@
<SearchBox @rendermode="InteractiveServer" />

@* WebAssembly — interactive in the browser *@
<ShoppingCart @rendermode="InteractiveWebAssembly" />

@* Auto — Server first, then WASM once downloaded *@
<Dashboard @rendermode="InteractiveAuto" />

Static SSR is the default, which matters: most of a page is usually not interactive, and rendering it as plain HTML is faster and cheaper than either interactive mode.

Auto mode is the clever one. The first visit uses Blazor Server, so the page is interactive immediately without waiting for a multi-megabyte download. In the background the WASM runtime downloads and caches; subsequent visits run entirely client-side.

⚠️ Auto has a real complication: the component runs in two different places at different times. A component that injects a DbContext works on the server and fails on WebAssembly. So components using Auto must be written for the client model — calling an API — even on the visits where they happen to run on the server.

How it actually works — prerendering

By default, interactive components are prerendered: the server renders the initial HTML so the page appears immediately, then interactivity connects.

⚠️ That means component initialisation runs twice — once during prerender, once when interactivity starts:

@code {
    protected override async Task OnInitializedAsync()
    {
        // ⚠️ Runs TWICE: once prerendering, once after connecting
        Data = await Http.GetFromJsonAsync<Data>("api/data");
    }
}

Two API calls per page load, and any side effect happens twice. The usual fix is PersistentComponentState, which carries the prerendered data across so the second run reuses it.

This is the single most common Blazor surprise, and worth being able to name.

Choosing

Situation Mode
Internal line-of-business app, good network Server
Public site, must work offline or on poor networks WebAssembly
Mostly static content with interactive islands Static SSR + islands
Want fast first load and client-side afterwards Auto
Public consumer site, large front-end team ⚠️ Consider React/Vue instead

⚠️ Blazor Server does not scale like a stateless API. Each user holds server memory and a persistent connection, so capacity planning is per concurrent user rather than per request. A SignalR backplane such as Redis is needed once you run more than one instance (21.4).


Code

A component showing the shape:

@* Components/OrderList.razor *@
@rendermode InteractiveServer
@inject IOrderService Orders
@implements IDisposable

<h3>Orders</h3>

<input @bind="searchTerm" @bind:event="oninput" placeholder="Search" class="form-control" />

@if (orders is null)
{
    <p><em>Loading…</em></p>
}
else if (orders.Count == 0)
{
    <p>No orders found.</p>
}
else
{
    <table class="table">
        @foreach (var order in orders)
        {
            <tr @key="order.Id">                    @* @key helps the diffing algorithm *@
                <td>@order.Reference</td>
                <td>@order.Total.ToString("C")</td>
                <td><button @onclick="() => Cancel(order.Id)">Cancel</button></td>
            </tr>
        }
    </table>
}

@code {
    private IReadOnlyList<OrderSummary>? orders;
    private string searchTerm = "";
    private CancellationTokenSource? cts;

    protected override async Task OnInitializedAsync() => await LoadAsync();

    private async Task LoadAsync()
    {
        // Cancel the previous search — otherwise fast typing races
        cts?.Cancel();
        cts?.Dispose();
        cts = new CancellationTokenSource();

        try
        {
            orders = await Orders.SearchAsync(searchTerm, cts.Token);
        }
        catch (OperationCanceledException)
        {
            // A newer search superseded this one — normal, not an error
        }
    }

    public void Dispose()
    {
        cts?.Cancel();
        cts?.Dispose();                             // ⚠️ essential — see below
    }
}

Three things worth explaining:

@key on the loop helps Blazor's diffing algorithm match elements between renders. Without it, reordering a list can reuse the wrong DOM elements and produce visibly wrong state.

Cancelling the previous search matters because @bind:event="oninput" fires per keystroke. In Blazor Server that is a round trip per character, so without cancellation, results can arrive out of order and an older search overwrites a newer one (2.14).

IDisposable is not optional. A Blazor Server component lives as long as the circuit, and undisposed subscriptions or token sources leak per user (3.5).

⚠️ That @bind:event="oninput" is itself questionable in Server mode — a round trip per keystroke. Debouncing, or binding on onchange instead, is usually the right call.


Interview questions

Q: What is Blazor and what are the render modes?

Blazor builds interactive web UI with C# components instead of JavaScript. The two original models are Blazor Server, where components run on the server and UI diffs travel over a SignalR WebSocket, and Blazor WebAssembly, where a .NET runtime compiled to WASM runs the components in the browser. .NET 8 added the Blazor Web App template, which unifies them — each component declares a render mode, with static server rendering as the default and interactive Server, WASM or Auto opted into per component.

Q: What are the trade-offs between Server and WebAssembly?

Server has a tiny initial download and starts fast, and it can talk directly to a database because the code runs on the server — but every interaction is a network round trip, so it is latency sensitive, and the server holds a circuit in memory per connected user, which changes how you plan capacity. WebAssembly has no per-user server cost and no latency on interactions since it runs locally, and it can work offline — but the first load downloads several megabytes of runtime, and your code is on the client, so no secrets and every authorization check must be repeated server-side.

Q: What is Auto render mode?

The first visit uses Blazor Server, so the page is interactive immediately without waiting for the WebAssembly runtime, while that runtime downloads and caches in the background. Subsequent visits run entirely client-side. It gives you a fast first load and no ongoing server cost. The complication is that a component then runs in two different places at different times, so it must be written for the client model — calling an API rather than injecting a DbContext — even on visits where it happens to run on the server.

Q: What is the most common Blazor surprise?

Prerendering causing initialisation to run twice. Interactive components are prerendered server-side so the page appears immediately, then run again when interactivity connects — which means OnInitializedAsync executes twice, so you get two API calls per page load and any side effect happens twice. PersistentComponentState is the usual fix, carrying the prerendered data across so the second run reuses it rather than refetching.


Traps ⚠️

  • Prerendering runs initialisation twice. Two API calls, duplicated side effects.
  • Blazor Server holds memory and a connection per user, so capacity is per concurrent user.
  • A dropped Blazor Server connection loses UI state. Common on mobile.
  • WebAssembly code is downloadable and decompilable. No secrets; repeat every authorization check server-side.
  • WebAssembly cannot reach a database. It needs an API.
  • Auto mode components run in both places, so they must be written for the client model.
  • Missing @key in a loop lets the diffing algorithm reuse the wrong elements.
  • Undisposed subscriptions leak per circuit in Blazor Server.
  • @bind:event="oninput" in Server mode is a round trip per keystroke.
  • Blazor Server needs a SignalR backplane to run more than one instance.


Sources