Blazor Fundamentals: Building Web Applications with Blazor and .NET 9

Blazor Fundamentals: Building Web Applications with Blazor and .NET 9

13+

Build powerful, modern web applications using only C#.

0 followers
77 chapters
Software & AI Development
2025
You're viewing a limited preview. Create a free account to read free books or get Premium to unlock the entire library.
In this chapter, you'll set up your development environment, install and configure essential tools, and create your first Blazor Web App using the latest unified hosting model introduced in .NET 8 and improved in .NET 9. 

By the end of this chapter, you will:
• Install Visual Studio 2022 or later
• Configure Visual Studio specifically for Blazor development
• Create your first Blazor Web App using the new template
• Understand the structure of a Blazor project
• Learn how to run and debug your Blazor application

Visual Studio Installation

Step-by-Step Walkthrough:
1. Go to the official Visual Studio download page: https://visualstudio.microsoft.com/downloads/.
2. Select the Community Edition (free and recommended).
3. Run the downloaded installer and follow the on-screen instructions.
4. During installation, select the ASP.NET and web development workload to ensure Blazor support.

Configuring Visual Studio for Blazor
After installation, ensure Visual Studio is properly configured:
1. Open Visual Studio.
2. Navigate to Tools > Get Tools and Features if additional workloads or components are needed.
3. Confirm that the ASP.NET and web development workload is installed.



Creating Your First Blazor Web App

.NET 9 introduces a new default Blazor project template called Blazor Web App. This unified template allows you to mix server-side and client-side interactivity within a single project, eliminating the need to choose between Blazor Server or Blazor WebAssembly up front.

Step-by-Step Walkthrough:
1. Launch Visual Studio and select Create a new project.
2. In the project templates, search for "Blazor Web App".
3. Choose Blazor Web App from the list.
4. Name your project, select a save location, and click Next.
5. Ensure .NET 9 is selected as the Framework.
6. Choose the desired interactive render mode (e.g., Server for this book’s examples).
7. Leave authentication as "None" and click Create.

Exploring Blazor Project Structure

After creating your Blazor Web App project in .NET 9, take a moment to understand the layout and purpose of each folder and file. This structure is designed for clarity, modularity, and performance.

• wwwroot: This is the web root of your application. It holds static assets that are served directly to the browser, such as CSS files, JavaScript libraries, fonts, and images. Files in this folder are publicly accessible.
• Components: This folder contains reusable UI components, grouped by function and purpose. It is a key part of organizing a Blazor Web App.
    • Pages: Contains .razor files that represent navigable pages in your app. Each file typically includes a @page directive and represents a routable view, such as /index or /login.
    • Layout: Holds shared layout components, such as MainLayout.razor, Header.razor, or NavMenu.razor. These are used to define consistent structure across pages, such as headers, navigation bars, and footers.
    • App.razor: Serves as the root component for your Blazor application. It contains the Router and layout definition, which handle routing logic and determine which components to display based on the current URL.
    • Routes.razor: Introduced in .NET 9, this file centralizes route configuration for your app. You can define available routes and associate them with their corresponding components, making routing easier to manage in large apps.
• Program.cs: This is the entry point of your Blazor Web App. It sets up services, configures the rendering mode (e.g., Server or WebAssembly), and registers root components. You will often modify this file to add services to the dependency injection container.


Running and Debugging Blazor Applications

Getting your Blazor Web App up and running is simple with Visual Studio. Here's how to launch and debug your application:

1. Start the App
Press F5 or click the green Run button to build and launch your application.

2. View in Browser
Your default web browser will open automatically, displaying your Blazor app.

3. Set Breakpoints for Debugging
You can set breakpoints in your Razor files or C# classes:
o Click in the margin next to a line number to place a breakpoint.
o When the application hits a breakpoint, Visual Studio will pause execution.
o You can then inspect variables, step through code, and evaluate the current application state.

4. Stop the App
To end the running session, click the red square (Stop button) in the toolbar.


Congratulations. You have just run and debugged your first Blazor application.

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.