liveBook · Manning

Pages folder

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

The Pages folder is the conventional ASP.NET Core project directory that contains Razor Pages and their supporting Razor configuration files.

The Pages folder is the conventional home for Razor Pages in an ASP.NET Core project. Put a page such as Index.cshtml there; its file name and location relative to Pages determine its URL by default. Pages/Index.cshtml handles /index unless its route is overridden. section 23.2.2

A Razor Page starts with @page so it is treated as a page rather than a controller view. Its @model directive selects a page model; an example defines that model alongside the markup with @functions. The folder gives the file its conventional location, while these directives determine how it behaves. section 23.2.2

Place supporting Razor files in Pages when setting up shared page behavior:

  • _ViewImports.cshtml supplies namespaces and enables tag helpers for page markup. section 32.6
  • _ViewStart.cshtml selects a default layout, such as _Layout.cshtml, which can also live in Pages. section 32.6

A page at a deliberate URL

Create Pages/Index.cshtml with @page "/pages" to serve /pages rather than relying on the file-based default. A product-list page at that URL can render database results and link to other Razor Pages. In the application setup, AddRazorPages registers the services and MapRazorPages registers page endpoints. section 31.3 section 8.2.1

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

How deep it goes
Explains folder creation, file-based URL routing, and placement of imports and layout files; routing overrides receive only a brief mention.
Examples
Uses Index.cshtml pages for a product display, a product-list form application, and a people table.
What it assumes
Assumes familiarity with Razor syntax while showing how to create the folder and its page and configuration files.
What you can do after
Operate/configure it by placing pages and shared Razor files in Pages and choosing default or explicit page URLs.
How it connects
Connects Razor Pages to URL routing, page models, tag helpers, and Razor layouts.

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.