liveBook · Manning

Razor Pages

As explained in .NET in Action, Second Edition

A Razor page is an ASP.NET Core web page that combines view rendering with request-handling logic without requiring an explicit controller.

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

How .NET in Action, Second Edition covers it

How deep it goes
Explains directives, routing setup, PageModel conventions, and working GET and POST implementations, but covers only basic Razor syntax.
Examples
Demonstrates home and error pages plus an ASCII-art form using Figgle and a shared navigation layout.
What it assumes
Assumes intermediate C# and basic web concepts while teaching page directives, MVC terminology, request handlers, and project structure.
What you can do after
Build it from scratch as a functional ASP.NET Core page with markup, code-behind, GET or POST handlers, and navigation.
How it connects
Connects Razor syntax with ASP.NET Core, MVC, PageModel, HTTP GET and POST handlers, HTML forms, and dependency or package configuration.

Also explained in

Compare how 3 books explain Razor Pages →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.