Razor Pages build page-based, server-rendered ASP.NET Core applications. Each page handles a URL and generates HTML for a browser. They suit sites people use directly, rather than APIs that primarily return data to other software; an application can also offer both. section 1.2 section 13.0
Start with the ASP.NET Core Web App template, then run the project. Its Home and Privacy links open two working pages. In Program.cs, AddRazorPages() registers the required services and MapRazorPages() registers the page endpoints. The surrounding middleware handles concerns including HTTPS redirection, static files, routing, and authorization. section 13.1.1 section 13.1.2
Listing 13.2 The Program.cs file for a Razor Pages app
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); #1 WebApplication app = builder.Build(); if (!app.Environment.IsDevelopment()) #2 { #2 app.UseExceptionHandler("/Error"); #2 app.UseHsts() #2 } #2 app.UseHttpsRedirection(); #3 app.UseStaticFiles(); #3 app.UseRouting(); #3 app.UseAuthorization(); #3 app.MapRazorPages(); #4 app.Run(); #1 Registers the required services to use the Razor Pages feature #2 Conditionally adds middleware depending on the runtime environment #3 Additional middleware can be added to the middleware pipeline. #4 Registers each Razor Page as an endpoint in your application
The template's Program.cs shows where Razor Pages services and endpoints fit in the request pipeline. section 13.1.2
A page is a .cshtml file in the Pages folder, usually paired with a PageModel class in a similarly named .cshtml.cs file. Put @page on the first line so ASP.NET Core recognizes the file as a page endpoint; @model identifies its PageModel. Razor syntax combines ordinary HTML with C# values to produce the response. section 13.1.3 section 13.5
A Privacy page
Pages/Privacy.cshtml handles /Privacy by default. It can set ViewData["Title"] in a C# block, display that value in an <h1>, and render an ordinary paragraph. Its companion Privacy.cshtml.cs holds the PageModel. section 13.1.3 section 13.5
Routing selects a page from the request URL; then a page handler on its PageModel runs. Handler names indicate HTTP verbs: OnGet handles GET. Model binding supplies request values to handler parameters or bound properties. A handler can call an injected service for business logic, place results on its PageModel, and return Page() to render the associated view. Keep business logic in services rather than in handlers. section 13.2 section 13.4.1 section 13.5
By default, a page's path under Pages becomes its route: Pages/Products/View.cshtml maps to /Products/View. An Index.cshtml page also gets a route without /Index. Customize a page's route with its @page directive, or adjust application-wide URL conventions when file-based URLs are not what you want. Validate user-provided values before using them. section 14.3 section 14.7 section 16.0
Razor Pages use the Model-View-Controller pattern and build on ASP.NET Core MVC, but organize the work around individual pages and their handlers. That makes a page's URL, display, and request-handling code straightforward to find together. section 13.4 section 13.5


