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.
  • RouteView shows the page inside a layout (the frame with the menu). DefaultLayout picks that layout.
  • NotFoundPage is 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 @page line.
  • A part in braces is a route parameter: @page "/tea/{Name}". Visiting /tea/masala sets the page's [Parameter] Name to "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=value pairs, 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, like public int Cups { get; set; }. Blazor fills it from the query string: cups=2 sets Cups to 2.

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)

PatternExampleMatches
Multiple routestwo 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 :int checks the type. If the address does not fit, the page does not match.
  • The parameter type must match the constraint: {Id:int} needs public 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.razor with @page "/not-found".
  • Routes.razor points to it with NotFoundPage="typeof(Pages.NotFound)".

Simple lesson: The Router picks a page by its @page pattern. Braces make parts of the address into parameters.

NavLink

  • NavLink is 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 class active.
  • The template's side menu (NavMenu.razor) uses NavLink for Home, Counter and Weather.

Active Link Styling

  • Style the active class 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), /menu is 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/

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.
  • FocusOnNavigate moves focus to the element you choose with Selector, 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.razor has <HeadOutlet />, which writes it into the page's head.

HeadContent

  • <HeadContent> adds other tags to the head, 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".
  • Placeholder is 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

MistakeWrong ideaCorrect ideaWhat goes wrong
Using <a> for menu linksA plain link highlights itselfUse NavLink and style .activeThe open page is never highlighted
Home link without Match="NavLinkMatch.All"/ only matches the home pageAdd Match="NavLinkMatch.All"Home looks active on every page
Two pages with the same @page addressBlazor picks oneGive every page its own addressAn error about ambiguous routes
Virtualize without a fixed heightIt knows the screen sizePut it in a box with a height and overflow-y: autoAll rows render, or none scroll
Expecting ErrorBoundary to fix the errorIt makes the error go awayIt only shows a message. Fix the real bugThe same error comes back
Reading a query value without a defaultThe value is always thereUse ?? "not given"null where you expected text

Check Yourself

Exercises

What to Remember

  • Router (in Routes.razor) shows the page whose @page pattern matches the address. {Name} makes a route parameter.
  • NavLink adds active to the link for the open page. FocusOnNavigate moves focus after a page change.
  • PageTitle sets the tab title. HeadContent adds head tags.
  • Virtualize draws only visible rows. ItemsProvider loads rows on demand.
  • ErrorBoundary keeps one error from breaking the whole page. Recover() resets it.

Next: Component Lifecycle