liveBook · Manning

Blazor

As explained in Pro ASP.NET Core 7, Tenth Edition

The Blazor model is the programming model in which .NET components manage web user interfaces, rendering, and user interactions.

Blazor is an ASP.NET Core programming model for interactive web interfaces built with C# and Razor Components. Blazor Server runs application code on the server: JavaScript receives browser events, forwards them over a persistent connection, and displays the resulting changes. Blazor WebAssembly runs the C# application code in the browser. The two approaches have different trade-offs; neither fits every project. section 1.1.3 section 33.0

The practical attraction is keeping interface behavior in C# rather than adopting a JavaScript framework. For Blazor Server, that choice depends on reliable connectivity and modern browsers: interaction stops when the connection is lost. A line-of-business application with known browsers and network quality is a better candidate than a public-facing site where neither can be assumed. section 33.2.1 section 33.2.3

A Blazor Server interface is assembled from components and ASP.NET Core configuration:

  • Configure Blazor services and its endpoint with AddServerSideBlazor and MapBlazorHub, and include the required JavaScript file. section 33.0
  • Use Razor Components for markup and C# behavior. The @page directive makes a component routable; event attributes connect browser interactions to handlers. Components also support data binding and code-behind classes. section 10.4 section 33.0
  • Use a Router to select a component from the current URL, a layout for shared structure, and NavLink for navigation without a new HTTP request. section 10.1.3
Listing 10.4 The Routed.razor File in the SportsStore/Pages/Admin Folder
<Router AppAssembly="typeof(Program).Assembly">
   <Found>
       <RouteView RouteData="@context" 
           DefaultLayout="typeof(AdminLayout)" />
   </Found>
   <NotFound>
       <h4 class="bg-danger text-white text-center p-2">
           No Matching Route Found
       </h4>
   </NotFound>
</Router>

The administration router shows the decision point: render the matched component with a default layout, or display a no-match message. section 10.1.3

Build an administration interface

For a product catalog, provide a list interface and an edit interface to create, read, update, and delete products. Built-in form components support the edit side: EditForm contains input components, while validation attributes and form events handle validation. Align repository and component lifetimes with OwningComponentBase<T> when the component owns the service it uses. section 10.3 section 36.0 section 10.4

Watch what rendering does to data access. Blazor rerenders a component after a state change to determine what must change in the browser. If its markup reads an Entity Framework Core query-backed property, even an unrelated button click can execute the query again. In a list example, checking Count and iterating the same property produces two identical database queries per render. Manage query execution explicitly rather than treating a LINQ query as an already-loaded result. section 36.3.2 section 36.0

Plan for the Server connection to fail. A custom components-reconnect-modal element and CSS can distinguish reconnection attempts, failures, and rejected connections; stopping or restarting the server makes those states observable. To protect an entire application, apply Authorize to its entry-point Razor Page so unauthenticated requests reach the login prompt instead. section 34.4.1 section 39.4

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through configuration, components, events, forms, connection failures, and rerender-driven database queries, while treating WebAssembly separately.
Examples
Uses product-catalog CRUD screens, a routed administration layout, connection-error messages, and a button that exposes repeated database queries.
What it assumes
Assumes familiarity with C# and ASP.NET Core conventions while introducing Blazor Server configuration and component basics.
What you can do after
Build it from scratch with Blazor Server components, then diagnose connection loss and avoid unnecessary Entity Framework Core queries.
How it connects
Connects Razor Components, ASP.NET Core routing, Entity Framework Core, form validation, and authorization.

Also explained in

Compare how 2 books explain Blazor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.