liveBook · Manning

Routing

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

Router is a Blazor component that discovers routable components in an assembly and renders the component matching the current URL.

Blazor component routing selects which Razor component to display as the URL changes. It uses ASP.NET Core routing, but a Router component handles selection within the Blazor application, allowing different components to appear without a new HTTP connection. section 35.0 section 35.2

Listing 35.3 The contents of the Routed.razor file in the Blazor folder
<Router AppAssembly="typeof(Program).Assembly">
   <Found>
       <RouteView RouteData="@context" />
   </Found>
   <NotFound>
       <h4 class="bg-danger text-white text-center p-2">
           No Matching Route Found
       </h4>
   </NotFound>
</Router>

In Routed.razor, AppAssembly identifies the assembly to search. Found passes the matched RouteData to RouteView; NotFound displays a message when no component matches. section 35.2

The pieces have separate jobs:

  • Set AppAssembly on Router, commonly to typeof(Program).Assembly, so it can find routable components. section 35.2
  • Define component routes with @page; put RouteView in Found to display the matched component and support shared content through layouts. section 35.2 section 35.5
  • Provide NotFound content for an unmatched URL, and use NavLink to navigate between routed components. section 35.2 section 35.5

Do not confuse Router with the endpoint-routing middleware that matches an HTTP request to an endpoint. Router bridges that routing system to Blazor component display; RouteView performs the rendering for a match. section 13.1.1 section 35.2

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

How deep it goes
Works through assembly configuration, Found and NotFound fragments, and the RouteView handoff; stops short of route-matching internals.
Examples
Configures Routed.razor with the current assembly, a RouteView for matches, and a visible unmatched-route message.
What it assumes
Assumes an existing Blazor application and introduces Router configuration through a new Razor component.
What you can do after
Build it from scratch with Router, RouteView, AppAssembly, Found, NotFound, and @page to select components by URL.
How it connects
Connects ASP.NET Core routing with Razor components, @page directives, RouteView, layouts, and NavLink.

Also explained in

Compare how 22 books explain routing →

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.