What Is Blazor?
By the end of this lesson
Explain what Blazor does and which hosting models exist.
Blazor is a framework for building interactive web interfaces where the components are written in C#. The browser still shows HTML and applies CSS. What changes is where the interaction logic lives: in C# compiled alongside the rest of your solution, rather than in JavaScript maintained separately from it.
Interactive has a specific meaning here. The page responds to what someone does without reloading. A button expands a row. Typing in a search box narrows a list. Saving a form puts a message beside the field that is wrong. Without a framework, each of those needs either a full round trip and a page reload, or a piece of JavaScript. Blazor offers a third option: you write a C# method and the framework arranges for it to run when the event happens.
For a team that already writes C# on the server, the appeal is one language and one set of models across the whole application. The class that describes an employee on the server can be the class that drives the edit screen. That is worth something. It is not free, and most of this lesson is about what it costs.
Five terms used constantly from here on:
- Component
- A self-contained piece of interface: its markup, the data it needs and the code that responds to events, in one file with a .razor extension. An employee row, a search box or a whole screen can each be a component.
- Rendering
- Working out what HTML a component should produce, and updating the page to match. Blazor renders a component when it first appears and again when something it shows has changed.
- Render mode
- The setting that decides where a component's C# runs — on the server or in the browser — and whether the component is interactive at all.
- WebAssembly
- A format browsers can execute alongside JavaScript. It is what makes it possible to run a .NET runtime inside a browser tab.
- SignalR
- A .NET library that keeps a two-way connection open between browser and server, so either side can send a message at any moment instead of the browser having to ask.
Blazor has two hosting models — two answers to the question of where your C# runs. They behave differently enough that the choice shapes the application:
| Blazor Server | Blazor WebAssembly | |
|---|---|---|
| Where your C# runs | On the server. The browser holds none of your application code. | In the browser, on a .NET runtime downloaded with the page. |
| Initial download | Small: markup, CSS and a script that opens the connection. | Larger, because the runtime and your assemblies come down too. Expect several megabytes on a first visit. |
| Cost of one interaction | A network round trip. The event goes to the server, your C# runs there, and the changed part of the page comes back. | Nothing on the network. The handler runs in the browser; only calls for data leave the machine. |
| Connection required | A live connection for the whole session. Lose it and the page stops responding until it reconnects. | One to load the application, then one only when it needs data. A short drop does not freeze the UI. |
| Latency the user feels | Roughly the round-trip time, on every click and keystroke you handle. Unnoticeable on an office network, felt on a slow mobile connection. | None from the framework once loaded. |
| Server cost | Every connected user holds component state in server memory, so the user count drives memory. | Low. The server hands out files and answers API calls. |
| Reaching server-only resources | Direct. A connection string or an internal service is available, because the code is already on the server. | None. Anything the browser has, the user has, so data arrives through an API you control. |
Those two models used to be a whole-project decision. You created either a Blazor Server project or a Blazor WebAssembly project, and every screen in it worked the same way.
Current .NET versions changed that. A Blazor Web App renders components on the server by default, with no interactivity, and lets you mark individual components as interactive — choosing the render mode per component. The employee list, which only reads and displays, can be plain server-rendered HTML. The edit screen, which has to respond to typing, can be interactive.
So the useful question is no longer which kind of Blazor application this is. It is which render mode this component needs, and the honest answer for a good number of components is none.
@page "/headcount"
@rendermode InteractiveServer
<h1>Headcount</h1>
<p role="status">Employees on record: @count</p>
<button type="button" @onclick="AddOne">Add one</button>
@code {
private int count = 214;
private void AddOne()
{
count++;
}
}- @page gives the component a route. Request /headcount and this is the component the router renders.
- @rendermode InteractiveServer asks for interactivity, with the C# running on the server over a SignalR connection. Without a render mode the markup still renders, but the button does nothing at all.
- @count writes the value of the field into the markup at that point. Any C# expression can follow the @.
- @onclick names the method to call. It is a C# method in the same file, with access to the same fields — there is no serialisation step for you to write and no JavaScript in between.
- After AddOne returns, Blazor renders the component again, compares the result with what it produced last time, and updates the one piece of text that changed. You never write the update.
- role="status" asks assistive technology to announce the new figure when it changes. A number that updates silently is a common accessibility gap in interactive interfaces, and it costs one attribute to avoid.
Summary
- Blazor builds interactive interfaces from C# components instead of moving the interaction logic into JavaScript
- Blazor Server runs your C# on the server over a live connection: small download, a round trip per interaction, direct access to server resources
- Blazor WebAssembly runs your C# in the browser: larger first load, no per-interaction round trip, and no access to anything the user should not see
- Current .NET lets you choose a render mode per component, so one application can mix server-rendered and interactive screens
- A component with no render mode renders once and never responds to events, which is silent and easy to miss
Practice
Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.
Think about it
Pick a model for each case
Three applications. For each, decide which hosting model fits and say what you are trading away.
One: a stock-count screen used on tablets in a warehouse where the wireless signal drops in the far aisles. Two: an HR portal used by twelve staff on the office network, reading from a reporting database in the same building. Three: a public pricing page that is mostly static text with one small salary calculator on it.
Show solution
The warehouse tablets point to WebAssembly. With Blazor Server, every tap in a dead spot either hangs or triggers a reconnect, and the person counting stock is the one who suffers. You pay with a slow first load, which matters far less on a device that opens the app once a shift.
The HR portal points to Blazor Server. Twelve users on a local network will not notice the round trip, the reporting database is reachable without building an API in front of it, and the small download means nothing to install or wait for. You pay by keeping state in server memory, which at twelve users is not a constraint.
The pricing page is the interesting one. Downloading a .NET runtime to power one calculator is poor value. Server-render the page and make only the calculator interactive, or use no framework at all for that one piece. The honest answer to "which Blazor?" is sometimes "almost none of it".
Try it yourself
Watch the render mode matter
Create a project with dotnet new blazor, add the Headcount component from this lesson, and remove the @rendermode line. Run the application and click the button.
Then put the line back and click again. What changed, and what did the browser tell you when the button did nothing?
Show solution
With no render mode the component is rendered once on the server as plain HTML. The button element exists, so it looks correct, and clicking it produces nothing — no error, no console message, no clue. That silence is why this mistake costs people an afternoon.
Adding the render mode connects the component to the interactive pipeline. The click now reaches your method, the field changes, and Blazor re-renders the component.
The lesson worth keeping: in a Blazor Web App, interactivity is opt-in per component. When a handler appears not to run, check the render mode before you check your code.
dotnet new blazor -o EmployeePortal
cd EmployeePortal
dotnet watch runKnowledge check
Nothing is recorded and there is no score. The explanation appears either way.
Saved in this browser only.