Razor Pages build ASP.NET Core web pages by combining HTML templates with .NET code and Razor directives. Each page handles requests without requiring an explicit controller, while application data and logic remain outside the view. section 3.2 section 3.2.1
The essential pieces are:
- `@page` marks a file as a routable Razor page; `@model` identifies its model type. section 3.2.1
- A `PageModel` code-behind class holds request-handling logic and page state, separating application logic from markup. section 3.2.2
- Handler methods follow HTTP verbs by convention: `OnGet` handles GET requests and `OnPost` handles form posts. section 3.2.2
- ASP.NET Core discovers handlers by name, executes them before rendering, and exposes state through the `Model` object. section 3.2.2
- Applications register Razor Pages with `AddRazorPages()` and expose them through `MapRazorPages()`. section 3.2
A form-backed ASCII art page
An `AsciiArt.cshtml` page declares `@page`, renders a POST form, and displays `Model.RenderedText` when available. Its `AsciiArtModel` implements `OnPost(string text)` and calls Figgle to transform the submitted text. A navigation link in `_Layout.cshtml` makes the page discoverable.
Listing 3.23 Contents of the AsciiArt code-behind file, AsciiArt.cshtml.cs
using Figgle;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace AsciiArtWebApp.Pages;
[ResponseCache(Duration = 0, #1
Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken] #2
public class AsciiArtModel : PageModel
{
public string? RenderedText { get; set; } #3
public bool ShowRenderedText => #4
!string.IsNullOrEmpty(RenderedText);
public AsciiArtModel() {} #5
public void OnPost(string text) #6
{
RenderedText = FiggleFonts.Standard.Render(
text ?? "Hello!"); #7
}
}
Inspect the complete code-behind implementation, including PageModel state, POST handling, response caching, antiforgery configuration, and Figgle rendering. section 3.2.2


