Blazor Routing: An Overview | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (28).NET MAUI  (216)Angular  (112)ASP.NET  (51)ASP.NET Core  (83)ASP.NET MVC  (90)Azure  (41)Black Friday Deal  (1)Blazor  (230)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (69)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (82)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (949)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (163)Xamarin  (160)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (158)Chart  (142)Chart of the week  (55)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (71)Development  (664)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (513)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (113)UI  (394)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (621)What's new  (334)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Blazor Router: An Overview

Blazor Routing: An Overview

Routing is a pattern-matching process that observes incoming client requests and decides what to do with each request. It refers to how we set up navigation and handle the application URL.

Like other SPA frameworks, Blazor has a router that manages virtual navigation. In addition, the Blazor server and WebAssembly apps allow routing with the help of various built-in components and services. So, in this article, I’ll go over all you need to know about routing in Blazor apps.

Router component in Blazor

Routing exists in Blazor to ensure that each request is directed to the best component with all the data necessary to display exactly what the user wants.

The Router component is used as a part of the App component in Blazor apps. The default implementation looks like this.

<Router AppAssembly="@typeof(Program).Assembly">
  <Found Context="routeData"><RouteView RouteData="@routeData"DefaultLayout="@typeof(MainLayout)"/>
    <LayoutView Layout="@typeof(MainLayout)">
       <p>Routing in Blazor Application.</p>

The AppAssembly defines which components to load for a given route. Looking at the code, we can spot two scenarios handled by Found and NotFound parameters, respectively:

  • The component with the given route is found.
  • The component with the given route is not found.

In the first case, when the component with the given route is found, the app will use the Found segment. After that, the RouteView component does all the magic for us. It receives the route data from the Router component, along with any parameters, and renders the required component together with its layout. It will use the default layout if the component doesn’t have a layout. We can set the default layout with the DefaultLayout property in the RouteView component.

In the second case, when the component with the given route is not found, the app will use the NotFound template. By default, it shows the p tag located within the LayoutView component, with a simple message informing the user that the desired page was not found. The LayoutView ensures that the specific layout gets rendered for the not found route. We can control which layout will be used by setting the Layout property of the component.

@page directive in Blazor routing

Routing rules are essential in Blazor routing since they let us define the exact component that should appear for a specified URL. We can set up these rules using the @page directive followed by the route itself.

For example, a component with the following declared code will load using the Router component if we make a request for

@page "/profile"

You can also define multiple route templates using multiple @page directives like in the following example. These routes should start with the / sign. Otherwise, the app will report an error.

@page "/profile"
@page "/myprofile"

If we navigate to the route (/profile), the given route will be matched by the Router component. Then, the profile.razor page component will be rendered.

These components have a particular folder within our app folder structures, usually the Pages folder. The assembly will be scanned at the app startup to find any components with the @page directive applied.

Route parameters and constraints

It is common to use the route parameters to pass data from one page to another. After defining the Router component, it can automatically populate the corresponding component property with the same name since they are case insensitive.

For example, we should define the route parameter salary in the following code segment (wrapped with curly braces {}) and a related property that must match the route parameter’s value, @countsalary. It can automatically populate the value of the route parameter.

@page "/profile/{salary}"

<p>monthly salary: @countSalary </p>
<button class="btn btn-primary" @onclick="IncrementSalary">see my profile </button>  

@code {
      private int countSalary = 50000;
      public string Salary { get; set; }
      private void IncrementSalary(){
          countSalary ++;

Then, you can run the app and enter any string in the address bar after /salary/ to see the route parameter value displayed.

monthly salary: 50000

Moreover, it is also possible to limit the salary parameter to accept only integer values.

@page "/profile/{salary:int}"


@code {
   public string Salary { get; set; }

Handling navigation

The NavLink component and the NavigationManager class are the two techniques for navigating among pages.

Navigation manager

In the C# code, we can use the NavigationManager class to manage URIs and navigation. It has the following properties, methods, and events:

  • Properties: BaseUri, Uri
  • Methods: NavigateTo, ToAbsoluteUri, ToBaseRelativePath
  • Events: LocationChanged

Let’s see their work using the following example. You can create a new Blazor component and inject the NavigationManager service using the @inject directive.

@page "/profile"
@inject NavigationManager nvm

<h3>Handling Navigation</h3>
<br />

<p>Absolute URI @nvm.Uri</p>
<p>Base URI: @nvm.BaseUri</p>

Then, check for the Uri and BaseUri values. The Uri property returns the page’s current absolute URI, while the BaseUri property returns the base URI.

Handling Navigation


NavLink component

If you don’t want to handle navigation programmatically and instead want to generate HTML hyperlinks, you can use the NavLink component. When the link’s href attribute matches the current URL, the NavLink component toggles an active CSS class, much like the HTML <a> element.

You can make it clear to the user which navigation link is for the current page by styling the active class. We have to use the NavLink component only for the navigation menu and not for links within the page’s content. The component has a Match property that NavLink uses to configure the match options. There are two possibilities:


The NavLink component will only be active if it matches the URL.

<NavLink class="nav-link" href="Profile"><span aria-hidden="true"></span> 


The NavLink component will be active whenever it matches any prefix of the current URL.

<NavLink class="nav-link" href="Profile" Match="NavLinkMatch.All"> 
  <span aria-hidden="true"></span> Profile

As an example, consider the link <NavLink href=”Profile” Match=”NavLinkMatch.Prefix”>. While we have for any location within the given URL, that link will be active.


This article covered routing features available in Blazor apps and various routing-related components and services available for developers. I hope my suggestions will help you define routes, parameters, and constraints easier than before.

Thank you for reading!

Syncfusion’s Blazor component suite offers over 70 UI components that work with both server-side and client-side (WebAssembly) hosting models seamlessly. Use them to build marvelous apps!

For questions, you can contact us through our support forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed