6.3 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|---|
Blazor routing | guardrex | Learn how to route requests in apps and about the NavLink component. | >= aspnetcore-3.0 | riande | mvc | 05/06/2019 | blazor/routing |
Blazor routing
By Luke Latham
Learn how to route requests in apps and about the NavLink component.
ASP.NET Core endpoint routing integration
Blazor server-side is integrated into ASP.NET Core Endpoint Routing. An ASP.NET Core app is configured to accept incoming connections for interactive components with MapBlazorHub
in Startup.Configure
:
Route templates
The <Router>
component enables routing, and a route template is provided to each accessible component. The <Router>
component appears in the App.razor file:
In a Blazor server-side app:
<Router AppAssembly="typeof(Startup).Assembly" />
In a Blazor client-side app:
<Router AppAssembly="typeof(Program).Assembly" />
When a .razor file with an @page
directive is compiled, the generated class is provided a xref:Microsoft.AspNetCore.Mvc.RouteAttribute specifying the route template. At runtime, the router looks for component classes with a RouteAttribute
and renders the component with a route template that matches the requested URL.
Multiple route templates can be applied to a component. The following component responds to requests for /BlazorRoute
and /DifferentBlazorRoute
:
<Router>
supports setting a fallback component to render when a requested route isn't resolved. Enable this opt-in scenario by setting the FallbackComponent
parameter to the type of the fallback component class.
The following example sets a component defined in Pages/MyFallbackRazorComponent.razor as the fallback component for a <Router>
:
<Router ... FallbackComponent="typeof(Pages.MyFallbackRazorComponent)" />
[!IMPORTANT] To generate routes properly, the app must include a
<base>
tag in its wwwroot/index.html file with the app base path specified in thehref
attribute (<base href="/">
). For more information, see xref:host-and-deploy/blazor/client-side#app-base-path.
Route parameters
The router uses route parameters to populate the corresponding component parameters with the same name (case insensitive):
Optional parameters aren't supported for Blazor apps in ASP.NET Core 3.0 Preview. Two @page
directives are applied in the previous example. The first permits navigation to the component without a parameter. The second @page
directive takes the {text}
route parameter and assigns the value to the Text
property.
Route constraints
A route constraint enforces type matching on a route segment to a component.
In the following example, the route to the Users component only matches if:
- An
Id
route segment is present on the request URL. - The
Id
segment is an integer (int
).
The route constraints shown in the following table are available. For the route constraints that match with the invariant culture, see the warning below the table for more information.
Constraint | Example | Example Matches | Invariant culture matching |
---|---|---|---|
bool |
{active:bool} |
true , FALSE |
No |
datetime |
{dob:datetime} |
2016-12-31 , 2016-12-31 7:32pm |
Yes |
decimal |
{price:decimal} |
49.99 , -1,000.01 |
Yes |
double |
{weight:double} |
1.234 , -1,001.01e8 |
Yes |
float |
{weight:float} |
1.234 , -1,001.01e8 |
Yes |
guid |
{id:guid} |
CD2C1638-1638-72D5-1638-DEADBEEF1638 , {CD2C1638-1638-72D5-1638-DEADBEEF1638} |
No |
int |
{id:int} |
123456789 , -123456789 |
Yes |
long |
{ticks:long} |
123456789 , -123456789 |
Yes |
[!WARNING] Route constraints that verify the URL and are converted to a CLR type (such as
int
orDateTime
) always use the invariant culture. These constraints assume that the URL is non-localizable.
NavLink component
Use a NavLink component in place of HTML <a>
elements when creating navigation links. A NavLink component behaves like an <a>
element, except it toggles an active
CSS class based on whether its href
matches the current URL. The active
class helps a user understand which page is the active page among the navigation links displayed.
The following NavMenu component creates a Bootstrap navigation bar that demonstrates how to use NavLink components:
There are two NavLinkMatch
options:
NavLinkMatch.All
– Specifies that the NavLink should be active when it matches the entire current URL.NavLinkMatch.Prefix
– Specifies that the NavLink should be active when it matches any prefix of the current URL.
In the preceding example, the Home NavLink (href=""
) matches all URLs and always receives the active
CSS class. The second NavLink only receives the active
class when the user visits the Blazor Route component (href="BlazorRoute"
).