Blazor · Foundations

Introduction to Blazor

Blazor lets you build interactive web pages with C# instead of JavaScript. One language, one set of tools, front end to back end.

Beginner 5 min Lesson 1 of 31Examples target .NET 9 Blazor Web App.
0/31 in Blazor
On this page · 5 min · Beginner

Learning objectives#

  • Explain what Blazor is and where it fits
  • Create and run a Blazor Web App
  • Read a simple Razor component
  • Compare Blazor with JavaScript frameworks

Explanation#

Blazor is part of ASP.NET Core. You build UI from Razor components: .razor files that mix HTML markup with C# code.

Components can render on the server (static or interactive over a SignalR connection) or run in the browser on WebAssembly. Since .NET 8 one Blazor Web App template supports all of these, chosen per page or component.

Because the same C# runs everywhere, you can share models, validation rules and services between the API and the UI.

Blazor suits teams that already know C#/.NET. React and Angular have larger ecosystems; Blazor wins on code sharing and tooling consistency.

Code example: Your first component#

Razor / MarkupComponents/Pages/Hello.razor Illustrative
@* dotnet new blazor -o ShopUi --interactivity Server && cd ShopUi && dotnet watch *@
@page "/hello"
@rendermode InteractiveServer


<h1>Namaste, @name!</h1>
<input @bind="name" @bind:event="oninput" placeholder="Your name" />
<p>You typed @name.Length characters.</p>


@code {
    private string name = "Techie Babaji";
}
Expected output
At /hello the heading updates on every keystroke: typing “Asha” shows “Namaste, Asha!” and “You typed 4 characters.”

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. dotnet new blazor creates a Blazor Web App; --interactivity Server enables interactive server rendering.
  2. @page makes the component reachable at /hello.
  3. @rendermode InteractiveServer makes it respond to events.
  4. @bind keeps the input and the name field in sync; the markup re-renders automatically.

Real-world use case#

An internal admin portal for a .NET company: the team writes UI, API and data access in C# and shares DTOs and validation between them.

Common mistakes#

Expecting events to work without an interactive render mode

In a Blazor Web App, pages are static by default. Buttons do nothing until the component uses an interactive render mode.

Thinking Blazor needs no HTML or CSS

Components are still HTML and CSS. You replace JavaScript for logic, not the web platform.

Best practices#

  • Start with the Blazor Web App template
  • Choose a render mode deliberately per page
  • Keep components small and focused
  • Use dotnet watch for hot reload

Remember this#

Blazor = web UI in C# with Razor components.

Static by default; add @rendermode for interactivity.

Same language and models front to back.

Practice exercise#

Your task: Add a button that resets the name to an empty string.

Show solution
Razor / Markup Illustrative
<button @onclick="() => name = string.Empty">Clear</button>

Mini quiz#

Introduction to Blazor quizMultiple choice Question 1 of 2

What file extension do Blazor components use?

Score so far: 0/0

Want more? Practise all Blazor MCQs · Blazor interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.