liveBook · Manning

Page directive

As explained in Pro ASP.NET Core 7, Tenth Edition

The @page directive is a Razor directive that assigns one or more URL routes to a Razor component or page.

The @page directive makes a Razor Page or Razor component reachable through URL routing. The syntax looks similar in both places, but the routing conventions differ: a Razor Page starts with a file-based route, while a component declares the URLs that display it. section 23.2.2 section 35.2.2

Choose the form that matches the file:

  • In a Razor Page (.cshtml), put @page first so the file is treated as a page rather than a controller view. A bare @page uses the route derived from the file’s location under Pages; Pages/Index.cshtml handles /index. section 23.2.2
  • Give a Razor Page a pattern such as @page "{id:long?}" to accept an optional, long-valued URL segment. The route value can supply an OnGetAsync(long id) parameter through model binding. An explicit path such as @page "/lists/suppliers" replaces that page’s file-based route. section 23.3.1
  • In a Razor component (.razor), repeat @page to support multiple URLs. For example, @page "/admin/products" and @page "/admin" display the same component; @page "/" can identify the component shown at the application’s default URL when a fallback route is configured. section 10.1.4 section 35.2.2

Do not assume route-pattern support is identical. The Razor Page pattern above accepts an optional segment. Component @page patterns do not support optional or catchall segment variables; declare two component routes when one must omit the segment. A component can receive a matched value through a same-named property marked [Parameter]. section 23.3.1 section 35.2.2 section 35.2.4

Listing 35.11 The contents of the PersonDisplay.razor file in the Blazor folder
@page "/person" 
@page "/person/{id:long}"

<h5>Editor for Person: @Id</h5>

<NavLink class="btn btn-primary" href="/people">Return</NavLink>

@code {

   [Parameter]
   public long Id { get; set; }
}

The component declares both /person and /person/{id:long}; its [Parameter] property receives the id from the second route. section 35.2.4

To add a second route to a Razor Page, use RazorPagesOptions.Conventions.AddPageRoute in Program.cs instead of repeating its @page directive. The added pattern supplements the route already defined for the page. After a routing change, restart ASP.NET Core and request each URL to check which page or component appears. section 23.3.2 section 35.2.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through file-based routes, explicit patterns, multiple routes, and segment values; defers detailed model binding.
Examples
Uses an Index.cshtml product lookup, administration URLs, and a person component with an id segment.
What it assumes
Assumes familiarity with Razor files and ASP.NET Core requests, while introducing the routing differences between pages and components.
What you can do after
Build it from scratch: assign and verify routes for Razor Pages and components, including URL segment values.
How it connects
Connects URL routing to Razor Pages, Razor components, model binding, and component parameters.

Also explained in

Compare how 2 books explain page directive →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.