Blazor Fundamentals: Building Modern Web Applications with .NET 11

Blazor Fundamentals: Building Modern Web Applications with .NET 11

13+

Build LibraryDesk with Blazor and .NET 11, from components and forms to server authorization, tests, accessibility, and deployment.

0 followers
37 chapters
Software & AI Development
2026
You're viewing a limited preview. Create a free account to read free books or get Premium to unlock the entire library.

Chapter 1: Welcome to Blazor and .NET 11

What You Will Learn

  • Explain where a Blazor component's C# code runs: on the web server or in the browser.
  • Tell apart static rendering, Interactive Server, Interactive WebAssembly and Interactive Auto.
  • Describe LibraryDesk, the app you build in this book, and read its first page.

Prerequisites

No additional prerequisites beyond those listed in About This Book. You don't need to install anything yet; tool setup comes next.

Concept Overview

Picture the first page you build, in Chapter 3: a heading, a number and an Add one button. Each click adds one to the number. In a plain HTML page that button does nothing, because HTML has no way to run your code. Something has to run when you click. Blazor lets you write that something in C#.

Blazor builds web pages from Razor components. A Razor component is a .razor file that mixes HTML markup with C#: the markup says what the page shows, and the C# holds the data and the code that runs when the user acts. A component can be as small as a button or as large as a whole page. Blazor is part of ASP.NET Core, the .NET framework for building web apps. ASP.NET Core runs the web server, sends each browser request to the right code, and provides services such as logging and sign-in. You still use HTML and CSS for structure and looks.

In this book you build LibraryDesk, a small public book catalog. It starts as an empty template in Chapter 2 and gains a page or a feature in each chapter: a counter, book details, favorites, a feedback form, a JSON API, sign-in, live notices, tests and a published copy. The catalog is a fixed list of sample books, and nothing leaves your computer, so you can experiment freely.

The most important idea in Blazor is where your component's C# code runs. You choose it for each component, and the choice is called its render mode. Figure 1.1 shows the four choices.

  • Static server-side rendering (static SSR): the server runs the component once for each request, turns the result into HTML and sends it. Links and form posts work, but a C# click handler never runs, because nothing stays running after the HTML is sent.
  • Interactive Server: the component stays alive on the server. The browser keeps a connection open to it, and each click travels to the server, which runs your C# and sends back only the parts of the page that changed. The component's state on the server plus that live connection is called a circuit. ASP.NET Core carries the messages with SignalR, its real-time messaging library (Chapter 19 uses it directly).
  • Interactive WebAssembly: the browser downloads a small .NET runtime and your component's code, and then runs them itself. WebAssembly is a standard part of every modern browser for running compiled code. Clicks are handled in the browser without a trip to the server.
  • Interactive Auto: the first visit uses Interactive Server while the browser downloads the WebAssembly files in the background. A later visit can use Interactive WebAssembly. A component already on the page keeps the runtime it started with; it does not switch when the download finishes.

Choose the render mode before you write the component.

Figure 1.1. A Blazor Web App can send static server-rendered HTML (static SSR), run interactive components on the server over a circuit, or run them in the browser with WebAssembly. Auto chooses one of the interactive runtimes when the component starts.

One page can mix these. A mostly static page can contain a few interactive components; each one is sometimes called an interactive island. You set a component's render mode with one line, such as @rendermode InteractiveServer, which you first write in Chapter 3. Chapter 16 compares all four modes side by side.

Use interactivity when C# must handle events or update the displayed page.

All of this lives in one kind of project, the Blazor Web App. It arrived in .NET 8 and is still the standard in .NET 11. With the Auto option this book selects in Chapter 2, it contains a server project and a client project. The client project holds the components that may run in the browser. A server-only choice needs no separate client project.

You may meet two older names online, Blazor Server and Blazor WebAssembly. They are hosting models, and before .NET 8 each one had its own project template. Blazor Server's template is gone: in a Blazor Web App, the same idea is the Interactive Server render mode. Blazor WebAssembly still has its own template, Blazor WebAssembly Standalone App. That app is a folder of static files with no server code, and Chapter 20 builds one for offline use. Inside a Blazor Web App, the same idea is the Interactive WebAssembly render mode, and this book uses the render mode names everywhere else. Blazor Hybrid is something else again: the same components inside a native desktop or mobile app. This book covers web apps only.

Step-by-Step Implementation

You have no project yet, so this chapter's steps are reading and deciding. They prepare the choices you make in code from Chapter 3 on.

1. Look at Figure 1.1 and think about the counter page from the start of this chapter. A click must run C#, so the page needs an interactive row. With static SSR alone, the button would appear but clicking it would do nothing.
2. Read LibraryDesk's Home page below. You add it to your project in Chapter 3. It has a title, a heading, two paragraphs and a link, and no button. Everything on it works with static SSR, because following a link is something plain HTML does.

Read LibraryDesk/Components/Pages/Home.razor

The @page line gives the page its address, "/" (the site's home). PageTitle sets the text on the browser tab. The rest is ordinary HTML.

@page "/"
<PageTitle>LibraryDesk</PageTitle>
<h1>LibraryDesk book examples</h1>
<p>
    A small public book catalog, built one chapter at a time
    from static pages, server events and browser code.
</p>
<p>
    It runs only on your computer. Feedback stays in memory,
    and nothing is sent anywhere.
</p>
<a href="/run">Start with a counter</a>

With the per-page/component setup selected in Chapter 2, no parent makes this page interactive. Without its own @rendermode line, Home therefore uses static SSR: the server renders it for the request and sends HTML.

3. Read the global.json file below. The .NET SDK is the set of tools that builds and runs .NET apps: the compiler, the project templates and the dotnet command. A global.json file tells the dotnet command which SDK version to use for every command run in its folder. "version": "11.0.100" asks for at least SDK 11.0.100, and "rollForward": "latestFeature" lets a later .NET 11 SDK stand in for it. You save this file in Chapter 2 after choosing .NET 11 in Visual Studio's project wizard, so subsequent builds of every project use the same SDK family.

Read global.json

{
    "sdk": {
        "version": "11.0.100",
        "rollForward": "latestFeature"
    }
}

Full Example

The two complete files below are LibraryDesk's starting point. You save global.json in the working folder in Chapter 2. Chapter 2 creates the Blazor Web App. In Chapter 3, you replace its Home page with this component.

global.json

{
    "sdk": {
        "version": "11.0.100",
        "rollForward": "latestFeature"
    }
}

LibraryDesk/Components/Pages/Home.razor

@page "/"
<PageTitle>LibraryDesk</PageTitle>
<h1>LibraryDesk book examples</h1>
<p>
    A small public book catalog, built one chapter at a time
    from static pages, server events and browser code.
</p>
<p>
    It runs only on your computer. Feedback stays in memory,
    and nothing is sent anywhere.
</p>
<a href="/run">Start with a counter</a>

The Blazor Web App template supplies the imports and HeadOutlet used by PageTitle. With the per-page/component setup selected in Chapter 2, Home inherits static SSR and needs no @rendermode line. Chapter 3 also adds /run, so the link has its destination.

Common Mistakes

  • Assuming Interactive Server keeps data private. The C# runs on the server, so the code feels hidden, but the rendered HTML, API responses and circuit messages all reach the browser. Decide what each page may show, and check access on the server (Chapter 11).
  • Assuming WebAssembly makes a site work offline. It is easy to believe because the code runs in the browser, but without a network the browser can't load the page in the first place. Working offline needs a published app and a cache, which Chapter 20 builds.
  • Making a whole page interactive because one button needs a click handler. It happens because the page is the first place you think of to put @rendermode. The whole page then holds a circuit or downloads WebAssembly. Keep the page static and give the small component that needs clicks its own render mode (Chapter 16).

Performance and Production Considerations

Each choice has a cost. Interactive Server uses server memory for every open circuit and needs a working network connection for every click. Interactive WebAssembly moves the work into the browser, but the first visit downloads more, and anyone can inspect the downloaded code and settings, so never put a password, key or other secret in client code. Static SSR keeps no circuit alive between requests, but each new request still runs server code. It can't run a C# click handler without an interactive component. None of them is always fastest: measure your real page before choosing one for speed. When I review a slow Blazor page, the first thing I check is which render mode each component on it uses.

Summary

A Razor component describes a piece of UI in HTML and C#. Its render mode decides where its C# runs: nowhere after the first response (static SSR), on the server over a circuit (Interactive Server), in the browser (Interactive WebAssembly), or a choice made when the component starts (Interactive Auto). LibraryDesk uses static pages for content and interactive components where events or ongoing updates must run C#.

Key Terms

  • Razor component: a .razor file that combines HTML markup and C# to produce part of a page.
  • Render mode: the setting that decides where a component's C# runs.
  • Static SSR: the server renders HTML once per request; no C# runs after that.
  • Circuit: a component's state on the server plus its live connection to the browser.
  • WebAssembly: a standard browser feature that runs compiled code; Blazor uses it to run the .NET runtime and your components in the browser.
  • Interactive Auto: starts with Interactive Server; a later visit can use Interactive WebAssembly once the files are downloaded. An existing component keeps its original runtime.
  • Blazor Hybrid: Razor components inside a native app, outside this book's scope.

Mini Quiz

1. A catalog page has a search box that must filter the list on every keystroke. Which render mode can't do that?

A. Interactive Server

B. Static SSR

C. Interactive WebAssembly

D. Interactive Auto

2. A visitor opens an Interactive Auto page for the first time. Where does the component's C# run on that visit?

A. In the browser, with WebAssembly

B. Nowhere, because an Auto page stays static until its files download

C. On the server, over a circuit, while the WebAssembly files download

D. In the browser's JavaScript engine

3. A mostly static page needs one Favorite button that updates a count. What does this chapter recommend?

A. Make the whole page Interactive Server

B. Keep the page static and give the button's component its own render mode

C. Make the whole page Interactive WebAssembly, so no circuit is needed

D. Keep the page static and give the button a C# click handler

4. A page rendered with static SSR contains <button @onclick="Add">Add</button> and void Add() => count++;, with count initially 0. What happens when the button is clicked?

A. Count stays 0, because no C# runs after the HTML is sent

B. Count becomes 1 on the server

C. Count becomes 1 in WebAssembly

D. Count becomes 1 after the page reloads

5. Anyone can download and inspect a WebAssembly app's files. Where must an API secret be kept?

A. In a client appsettings file

B. On the server, in code that never reaches the browser

C. In the client class constructor

D. In the compiled client code, because the browser can't read compiled code

Answers are provided in the Answer Key at the end of the book.

Activity

You have no project yet, so write this activity in a text file. You use the first part in Chapter 3.

  • Write a two-sentence description of your own public catalog as a paragraph element, in the same shape as the first paragraph of Home.razor above: <p>, your two sentences on the next lines, then </p>. The result is one <p> element that you can paste into a .razor file. In Chapter 3 you put it into LibraryDesk's Home page in place of its first paragraph.
  • For each of these features, write which row of Figure 1.1 it needs, and the render mode line its component would need, such as @rendermode InteractiveServer, or "no line" for a static feature: a link to the catalog page, a Favorite button that updates a count on the page, and a search box that filters the book list as you type. The result is three rows, each with a reason.
  • Compare your answers with Samples/Activities/Chapter01.md.

Enjoying this book?

Create a free account to leave comments, track your progress, and more!

Read and listen on the go with the unQbd app. Get the app
An unhandled error has occurred. Reload 🗙

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please reload the page.