Razor Pages Overview

Get familiar with Razor Pages applications.

Razor Pages is a feature in ASP.NET Core that simplifies building web applications by combining the power of the Razor view engine with a structured, page-focused programming model. It was introduced in ASP.NET Core 2.0 and has gained popularity for its simplicity and ease of use, particularly for building small to medium-sized web applications.

Here are some key features and concepts associated with Razor Pages in ASP.NET Core:

  • Page-oriented model: Razor Pages encourages developers to organize their code around individual web pages or views. Each Razor Page is a self-contained unit that combines the UI (HTML) and the associated logic (C# code) for that page.

  • The .cshtml file: Razor Pages uses .cshtml files (Razor views) to define the page's UI, just like a view in a traditional ASP.NET MVC. These .cshtml files can include both HTML markup, C# code, and Razor syntax that links the two together, making it easy to work with the page's content and logic in one place.

  • Page models: Each Razor Page has an associated page model, which is a C# class that contains the code and logic for the page. The page model handles user requests, processes data, and interacts with the Razor view. The C# code in a Razor Page is often called the "code-behind" because it handles events, data processing, and interaction with the Razor view.

  • Convention-based routing: Razor Pages follow a convention-based routing system, meaning that the URL structure of our application often directly maps to the file structure of our Razor Pages. This simplifies routing and URL management, making it easier to create SEO-friendly URLs.

  • Simplicity: Razor Pages are designed to be more straightforward and approachable than traditional ASP.NET MVC, making it a good choice for smaller applications or when we need a quick way to create web pages without the complexity of a full MVC architecture.

Razor Pages' project structure

The following playground contains a basic Razor Pages application that displays a greeting message on its home page:

Get hands-on with 1200+ tech skills courses.