What is the purpose of the App.razor and Routes.razor components in a Blazor project's overall structure?
Short answer
App.razor is the root component bootstrapping the entire Blazor application, typically containing a (or, in .NET 8+'s structure, referencing a separate Routes.razor) that scans for @page-decorated components and matches incoming navigation to the correct one, rendering a…
Advertisement
AModel answer
App.razor is the root component bootstrapping the entire Blazor application, typically containing a <Router> (or, in .NET 8+'s structure, referencing a separate Routes.razor) that scans for @page-decorated components and matches incoming navigation to the correct one, rendering a NotFound fallback for unmatched routes -- serving a similar bootstrapping role to how Program.cs configures the overall ASP.NET Core pipeline.
<!-- Routes.razor -->
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
</Found>
<NotFound>
<p>Sorry, that page doesn't exist.</p>
</NotFound>
</Router>Real-world example
A team customizes their NotFound fallback in Routes.razor to show a branded 404 page with navigation links back to key sections, rather than the default bare Blazor template's plain text message.
Likely follow-ups
- How does the Router discover which assemblies to scan for @page components?
- How would you customize the DefaultLayout used when no layout is explicitly specified?
Related topics