overview
- Documentation: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-7.0
router component
(In this section, Navigation is an injected NavigationManager)
The Router component enables routing:
App.razor
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
focus
Set the UI focus to an element based on a CSS selector. When the Router navigates to a new page, this sets the focus to the page’s top-level header:
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
provide custom content when content not found
Use the NotFound template:App.razor
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
tell router to search additional assemblies for components
Use the AdditionalAssemblies parameter:
<Router
AppAssembly="@typeof(App).Assembly"
AdditionalAssemblies="new[] { typeof(Component1).Assembly }">
@* ... Router component elements ... *@
</Router>
routing urls with dots
For hosted Blazor WASM and Blazor Server apps
- The default route template assumes that, if the last segment of a request URL contains a dot, a file is requested.
- Example: if the request URL is
/example/some.thingtheRouterinterprets this to be a request for the filesome.thing.
If some.thing is a route parameter value, the app must be configured with a custom route template.
- For Blazor WASM’s Server project, in
Program.cs:app.MapFallbackToFile("/example/{param?}", "index.html"); - For Blazor Server, in
Program.cs:app.MapFallbackToPage("/example/{param?}", "/_Host");
handling asynchronous navigation events
Router has an OnNavigateAsync handler. It is passed a NavigationContext. It is invoked when:
- A user visits a route for the first time by navigating to it directly in their browser
- A user navigates to a new route using a link or the
NavigationManager.NavigateToinvocation
App.razor
<Router AppAssembly="@typeof(App).Assembly"
OnNavigateAsync="@OnNavigateAsync">
...
</Router>
@code {
private async Task OnNavigateAsync(NavigationContext context)
{
// ...
}
}
OnNavigateAsync is executed twice: one when the endpoint component is initially rendered (statically), and again when the browser renders the endpoint component.
App.razorcan store theNavigationContextfor use inOnAfterRender{Async}, which has abool firstRenderthat can be checked.
handling cancellations
The NavigationContext passed to OnNavigateAsync contains a CancellationToken property. This CancellationToken is set whenever a new navigation event occurs. When implementing OnNavigateAsync, the callback
<Navigating> Content
The Router can indicate to the user that a page transition is occurring:
App.razor
@using Microsoft.AspNetCore.Components.Routing
<Router>
<!-- ... -->
<Navigating>
<p>Loading the requested page…</p>
</Navigating>
<!-- ... -->
</Router>