Skip to the workspace
Pebellslearn by building
ASP.NET Core Basics: Endpoints  ›  Capstone Project

Razor Pages and Razor Components — Rendering HTML

Razor Pages 15 minRazor Pages pair one page file with a handler class for request-response pages, while Razor Components are reusable UI pieces that can also run interactively.

aspnet core / Razor Pages and Razor Components — Rendering HTML

Explore each scene at your own pace.

01 · The rule

The rule

Razor Pages and Razor Components — Rendering HTML

ReadingRoom's API serves apps, but the librarians want web pages: a list of overdue loans, a search box. ASP.NET Core offers two HTML models built on the same Razor syntax — C# mixed into HTML with @. Knowing which fits saves you from forcing one into the other's job.

The rule

A page answers a request; a component is a reusable, possibly live, piece.

A Razor Page is one URL: a .cshtml file starting with @page, plus a PageModel class whose handlers run per request.

@page
@model OverdueModel
<h1>Overdue loans</h1>
<ul>
@foreach (var loan in Model.Loans)
{
    <li>@loan.BookTitle, due @loan.DueDate.ToShortDateString()</li>
}
</ul>
public class OverdueModel(LibraryDb db) : PageModel
{
    public List<LoanRow> Loans { get; private set; } = [];
    public void OnGet() => Loans = db.OverdueLoans();
}

The file Pages/Overdue.cshtml is served at /Overdue. OnGet handles GET, OnPost handles a form post. Switch it on with AddRazorPages() and MapRazorPages().

A Razor Component is a .razor file: markup plus an @code block. Components nest inside each other and take [Parameter] inputs. This is Blazor.

@rendermode InteractiveServer
<input @bind="term" @bind:event="oninput" />
<p>@Matches().Count() books match</p>

@code {
    [Parameter] public List<Book> Books { get; set; } = [];
    string term = "";
    IEnumerable<Book> Matches() => Books.Where(b => b.Title.Contains(term));
}

Since .NET 8, a component's render mode, set on it or inherited from a parent, decides how it behaves. With no interactive mode, it renders once on the server as static HTML. InteractiveServer keeps a live connection so clicks and typing run your C# on the server; InteractiveWebAssembly runs it in the browser; InteractiveAuto starts on the server and switches later.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.