Blazor tutorial
Build interactive web apps with C# and Razor components, step by step.
Course contents
Foundations
- Getting Started — What Blazor is and why teams use it, its key features, its place in .NET, versions, real uses, pros and cons, system requirements, installing the .NET SDK, setting up Visual Studio or VS Code, and creating your first Blazor Web App.
- Project Structure and Running the App — What every file and folder in a new Blazor Web App does: the .csproj, Program.cs, App.razor, _Imports.razor, wwwroot, the Components and Pages folders and launchSettings.json. Then how to run the app from the IDE or the CLI, and Hot Reload.
- Razor Syntax — How Razor mixes HTML and C#: the @ symbol and escaping it, @code blocks with variables and methods, implicit and explicit expressions, raw HTML with MarkupString, @if, @switch and loops, comments, @using and @namespace.
Blazor Components
- Components Basics — What a Blazor component is and how it renders, creating and using components, parent and child components, [Parameter], [EditorRequired], cascading values, ChildContent, EventCallback and CSS isolation.
- Built-in Components — Blazor's ready-made components: Router and @page routes, query strings and route patterns, the Not Found page, NavLink, FocusOnNavigate, PageTitle, HeadContent, Virtualize with ItemsProvider and ErrorBoundary.
- Component Lifecycle — The order of Blazor's lifecycle methods: OnInitialized(Async), SetParametersAsync, OnParametersSet(Async), route parameter changes, re-render triggers, StateHasChanged, ShouldRender, @key, OnAfterRender with firstRender, and Dispose.
- Layouts — Shared page frames in Blazor: LayoutComponentBase and @Body, the default layout, nested layouts, LayoutView, the NavMenu, sections with SectionOutlet and SectionContent, and the layout's error UI.
User Interaction
- Forms & Events — Forms and events in Blazor: input components with @bind-Value, one-way and two-way binding, click, mouse, keyboard, focus and change events, EditForm and models, custom inputs, OnSubmit and resetting a form.
- Form Validation — Form validation in Blazor: DataAnnotationsValidator, validation attributes and custom error messages, ValidationSummary and ValidationMessage, styling messages, OnValidSubmit and OnInvalidSubmit, custom attributes, cross-field rules and clearing messages.
Render Modes
- Server Rendering — Server render modes in Blazor: the four render modes, Static SSR with streaming and enhanced navigation, Interactive Server with @rendermode, SignalR and circuits, reconnection, component and app state, prerendering and PersistentComponentState.
- WebAssembly & Auto — Browser render modes in Blazor: Interactive WebAssembly project setup, @rendermode, the runtime and rendering flow, Interactive Auto and its server-to-WebAssembly switch, state across render modes and how to choose a render mode.
Browser Integration
- JavaScript Interop — JavaScript interop in Blazor: when to use it, adding JavaScript files, IJSRuntime with InvokeAsync and InvokeVoidAsync, arguments and return values, sync calls, modules, interop timing, [JSInvokable] and DotNetObjectReference, JSException and disposing references.
Data Access
- Dependency Injection — Dependency injection in Blazor: dependencies and injection, what the DI container is and why it is used, registering in Program.cs, built-in dependencies, and @inject, [Inject] and constructor injection with NavigationManager.
- Services — Services in Blazor: what a service is and why it helps, separating logic from UI, a user service with CRUD methods, service interfaces, registering and injecting services, Transient, Scoped and Singleton lifetimes, and sharing logic across components.
- Database Handling — Databases in Blazor with EF Core: ORM basics, SQLite setup, connection strings and migrations, entity models, DbContext and DbSet, AddDbContextFactory, a data access service with CRUD and SaveChanges, and showing and changing records from components.
- API Integration — Build and use a Web API: an ASP.NET Core Web API with a Tea model, TeasController and routes, GET, POST, PUT and DELETE endpoints, request and response data, HttpClient and an API service in Blazor, CRUD from components, loading states and friendly error messages.
Application Security
- Middleware Pipeline — How every request passes through middleware: the ASP.NET Core request pipeline, Program.cs setup, execution order, built-in middleware (static files, HTTPS redirection, exception handling, status code pages), Use, Run and Map, short-circuiting, and inline and class middleware.
- Authentication — Find out who the user is: cookies and their security options, cookies in Blazor, the cookie authentication scheme and claims, ASP.NET Core Identity, password hashing with salt, register, login and logout, AuthenticationStateProvider, cascading authentication state and ClaimsPrincipal.
- Authorization — Decide what a user may do: protecting pages with [Authorize], AuthorizeRouteView, redirecting to login, AuthorizeView with Authorized and NotAuthorized content, role-based and claims-based authorization with policies, protecting API endpoints, and 401 vs 403.
- Threat Protection — Protect a Blazor app from common web threats: XSS and rendering untrusted HTML, CSRF and antiforgery tokens, HTTPS, HSTS and CORS policies, keeping secrets out of WebAssembly, and server-side validation.
Project & References
- Capstone Project - Tea Shop — Build Tea Shop, a complete food ordering app: project setup, layout and navigation, a Web API, ASP.NET Core Identity with registration, login and logout, secured pages and APIs, a menu, a shared cart service, checkout with validation, order history, roles and admin pages for food items and orders.
- Cheat Sheet — A short reference of the whole Blazor course: .NET CLI commands, Razor syntax, components and parameters, built-in components, lifecycle methods, forms and validation, render modes, JS interop, dependency injection, services, EF Core, HttpClient, the middleware pipeline, authentication and authorization, and threat protection.