Built-in Components
By Syncfusion®, Inc. · Last updated
Blazor's ready-made components: Router and @page routes, query strings and route patterns, the Not Found page, NavLink, FocusOnNavigate, PageTitle, HeadContent, Virtualize with ItemsProvider and ErrorBoundary.
What you will learn
- Explain how the Router matches @page routes and reads query strings
- Highlight the open page with NavLink
- Set the tab title with PageTitle and HeadContent
- Show long lists fast with Virtualize
- Catch errors with ErrorBoundary
Built-in Components Overview
- Built-in components are ready-made Blazor components provided by the framework.
- They help build common UI features without creating components from scratch.
- You use them like your own components, for example
<NavLink>or<Virtualize>. - Using built-in components reduces time and keeps applications consistent.
Real-World Example:
- Ravi needs menu links that show the open page.
- He needs a title in the browser tab.
- He needs a long list of past orders.
- Blazor's built-in components already do all of this.
The Router and page titles would change this website, so this lesson shows them with pictures and code to read. The other components run live in the code boxes.
Simple lesson: Built-in components do common jobs for you. Use them like any other component.
Router (Routes.razor)
- The Router looks at the web address and shows the page that matches it.
- It lives in
Components/Routes.razor, which you met in Project Structure and Running the App. RouteViewshows the page inside a layout (the frame with the menu).DefaultLayoutpicks that layout.NotFoundPageis the page to show when no page matches.
Pages and Routing (@page)
- A component with
@page "/menu"becomes a page at that address. - When you visit
/menu, the Router finds that component and shows it. - One component can have more than one
@pageline. - A part in braces is a route parameter:
@page "/tea/{Name}". Visiting/tea/masalasets the page's[Parameter] Nameto"masala".
Watch the animation. The Router reads the address and picks the matching page.
Animation: the address bar shows /menu, then /order. The Router compares the address with each page's @page line and highlights the matching page: Menu.razor for /menu, Order.razor for /order.
/menu
/order
Router
Menu.razor @page "/menu"
Order.razor @page "/order"
Home.razor @page "/"
The Router matches the address with each page's @page line.
Query Strings
- A query string is the part of a web address after
?, like/order?cups=2&tea=masala. - It holds extra values as
name=valuepairs, joined with&. - Use it for optional values, like a search word, a filter or a page number.
- In a page, add
[SupplyParameterFromQuery]to a property, likepublic int Cups { get; set; }. Blazor fills it from the query string:cups=2setsCupsto2.
The code box cannot change this website's address, so in the below example the address is a variable. The page reads cups and tea from it, the same way [SupplyParameterFromQuery] does for you.
What you see: "Cups: 2" and "Tea: masala". Try it: change the address to /order?cups=5 and press ▶ Run. Tea shows "not given", because the address has no tea value.
Route Patterns (Multiple, Optional, Constraints, Catch-All)
| Pattern | Example | Matches |
|---|---|---|
| Multiple routes | two lines: @page "/menu" and @page "/drinks" | /menu and /drinks show the same page |
| Optional parameter | @page "/order/{Cups:int?}" | /order and /order/3 |
| Constraint | @page "/order/{Id:int}" | /order/5, but not /order/abc |
| Catch-all | @page "/files/{*Path}" | /files/a/b/c, Path = "a/b/c" |
- A constraint like
:intchecks the type. If the address does not fit, the page does not match. - The parameter type must match the constraint:
{Id:int}needspublic int Id.
Common constraints: :int, :bool, :guid, :datetime and :decimal. Add ? to make a parameter optional: {Id:int?} needs public int? Id.
Not Found Page
- If no page matches, Blazor shows a Not Found page.
- The .NET 10 template has
Components/Pages/NotFound.razorwith@page "/not-found". Routes.razorpoints to it withNotFoundPage="typeof(Pages.NotFound)".
Simple lesson: The Router picks a page by its @page pattern. Braces make parts of the address into parameters.
NavLink
NavLinkis a link, like<a>, with one extra job: it knows when its page is open.- When the address matches its
href, it adds the CSS classactive. - The template's side menu (
NavMenu.razor) usesNavLinkfor Home, Counter and Weather.
Active Link Styling
- Style the
activeclass to highlight the open page, for example bold text or a coloured background. Match="NavLinkMatch.All"means "active only when the whole address matches". Use it for the home link"". Otherwise/would match every page.- Without it (the default,
Prefix),/menuis also active on/menu/specials.
In the below example, the links point to real pages of this website. NavLink compares each href with the address of the page you are reading now, and adds active to the links that match. The <style> block colours active links purple.
What you see: two purple links. This lesson matches the whole address. Blazor course (start of address) is also active, because the address starts with /learn/blazor. The same link with Match="NavLinkMatch.All" is not active, and neither are Home and Next lesson. Click a link: the box stays on this lesson and tells you which page a real app would open.
Animation: three menu links, Home, Menu and Order. As the page changes, the active highlight moves from Home to Menu to Order.
Home Menu Order
https://localhost/menu https://localhost/order The link for the open page gets class="active".
NavLink adds the active class to the link for the open page.
Simple lesson: Use NavLink for menu links. Style its active class to show where the user is.
FocusOnNavigate
- After a page change, a keyboard or screen-reader user should start at the new page's heading.
FocusOnNavigatemoves focus to the element you choose withSelector, such as"h1".- The template already has it in
Routes.razor(see the code under Router (Routes.razor) above).
Simple lesson: FocusOnNavigate helps keyboard and screen-reader users after each page change.
PageTitle
<PageTitle>sets the text in the browser tab.- Put it in any page. When that page opens, the tab shows its title.
- It works because
App.razorhas<HeadOutlet />, which writes it into the page'shead.
HeadContent
<HeadContent>adds other tags to thehead, such as a description for search engines.
Simple lesson: PageTitle sets the tab title. HeadContent adds tags to the head.
Virtualize
Virtualization Basics
- A list with 10,000 rows is slow if Blazor makes HTML for all of them.
<Virtualize>makes HTML only for the rows you can see, plus a few extra.- As you scroll, it swaps rows in and out. The list feels complete, but the page stays fast.
- The list must be inside a box with a fixed height and scrolling.
Animation: a long list of 10,000 orders. A window slides down the list. Only the rows inside the window are drawn as HTML; the rest are empty space.
10,000 orders (not drawn)
Order #… drawn Order #… drawn Order #… drawn
Green box = what you can see Only these rows become HTML. Scroll, and the rows change.
Virtualize draws only the rows in view.
In the below example, <Virtualize> shows 10,000 orders. The HTML inside it is one row, and order is the current item. The list sits in a scrolling box with a fixed height, which Virtualize needs.
What you see: a box you can scroll through all 10,000 orders. It scrolls smoothly, because only about 20 rows exist at a time.
On-Demand Loading (ItemsProvider)
- With
Items, the whole list must already be in memory. Ravi's 5,000 past orders live in a database, so loading them all at once is slow. - With
ItemsProvider, Virtualize asks for rows only when it needs them, like "give me rows 40 to 60". Placeholderis what each row shows while it loads.
In the below example, Virtualize calls LoadOrders with the rows it needs (StartIndex and Count). The method waits a moment, like a database, and returns those rows and the total, 5,000. Rows show "Loading…" until they arrive.
What you see: scroll fast, and rows show Loading… for a moment. The Console shows which rows were asked for, like "Loading rows 40 to 60".
Simple lesson: Virtualize draws only the visible rows. Use ItemsProvider to load rows as they are needed.
ErrorBoundary
Error Handling and Recovery
- Normally, if one component has an error, the whole page stops working.
<ErrorBoundary>puts a safety wall around part of the page. If a component inside it fails, only that part shows an error message. Everything outside keeps working.- Put the normal content in
<ChildContent>and the error message in<ErrorContent>. Recover()takes the wall down and shows the normal content again, so the user can try again.
In the below example, OrderBox.razor throws an error on the fourth click. Home.razor wraps it in ErrorBoundary, so only the box shows the error message. Try again calls Recover().
What you see: click Add cup four times. The order box is replaced by "Sorry, the order box had a problem", but the heading still shows. Click Try again to get a fresh order box.
ErrorBoundary is a safety net, not a fix. Still find and fix the real error. The Console shows its details.
Simple lesson: Wrap risky parts in ErrorBoundary, so one error does not break the whole page.
Common mistakes
| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
Using <a> for menu links | A plain link highlights itself | Use NavLink and style .active | The open page is never highlighted |
Home link without Match="NavLinkMatch.All" | / only matches the home page | Add Match="NavLinkMatch.All" | Home looks active on every page |
Two pages with the same @page address | Blazor picks one | Give every page its own address | An error about ambiguous routes |
| Virtualize without a fixed height | It knows the screen size | Put it in a box with a height and overflow-y: auto | All rows render, or none scroll |
Expecting ErrorBoundary to fix the error | It makes the error go away | It only shows a message. Fix the real bug | The same error comes back |
| Reading a query value without a default | The value is always there | Use ?? "not given" | null where you expected text |
Check Yourself
Exercises
What to Remember
Router(inRoutes.razor) shows the page whose@pagepattern matches the address.{Name}makes a route parameter.NavLinkaddsactiveto the link for the open page.FocusOnNavigatemoves focus after a page change.PageTitlesets the tab title.HeadContentaddsheadtags.Virtualizedraws only visible rows.ItemsProviderloads rows on demand.ErrorBoundarykeeps one error from breaking the whole page.Recover()resets it.
Next: Component Lifecycle