When and why to use Razor Pages
In this unit, you'll learn when and why to use Razor Pages for your ASP.NET Core application.
What Razor Pages are and the benefit they provide
Razor Pages is a server-side, page-centric programming model for building rich web UI with ASP.NET Core. Razor Pages provide the following benefits:
- Razor Pages makes it easy to get started building dynamic web applications when all you need is to define UI logic using a combination of HTML, CSS, and C#. Razor Pages provides a productivity advantage over the more complex Model-View-Controller (MVC) application model.
- Razor Pages encourages organization of files by feature, therefore easing maintenance of your application.
- Razor Pages can be broadly described as an HTML file where you can work with markup as you are used to, but you also have the advantage of adding server-side C# code by using Razor syntax. Razor pages have the extension .cshtml.
Razor Pages use a simple markup syntax called Razor for embedding server-based code into webpages. Razor syntax is a combination of HTML and C# where the C# code defines the dynamic rendering logic for the page. In a webpage that uses the Razor syntax, there can be two kinds of content: client content and server code:
- Server code: Razor syntax lets you add server code to your client content. If there is server code in the page, the server runs that code first, before it sends the page to the browser. By running on the server, the code can perform more complex tasks than using client content alone, like securely accessing server-based databases. Most importantly, server code can dynamically create client content — it can generate HTML markup or other content and send it to the browser along with any static HTML that the page might contain. From the browser's perspective, client content that's generated by your server code is no different than any other client content.
Separation of concerns in the PageModel
Razor Pages enforces separation of concerns for page-related data properties and logic operations in a C# PageModel class file. A model object typically defines data properties and encapsulates any logic or operations related to those data properties. A Razor Pages PageModel more specifically encapsulates the data properties and logic operations scoped just to its Razor page. It defines page handlers for requests sent to the page and for the data used to render the page.
When to use Razor Pages
Use Razor Pages in your ASP.NET Core application when:
- You want to generate dynamic UI for a browser from your ASP.NET Core application.
- You prefer a page-focused approach to developing web applications, where the page markup and PageModel are in close proximity.
- You want your page-focused ASP.NET Core application to use shared common HTML elements across your site and reusable partial views.
Razor Pages allow you to keep your ASP.NET Core pages organized in a simpler way:
- All view (page) specific logic and page properties defined in the Razor page's (PageModel) can be kept together in their own namespace and directory.
- Groups of related pages can be kept in their own namespace and directory.
ASP.NET Core also supports the MVC pattern for building web applications. The Model defines the fundamental behaviors and data for the application and its components. The View uses HTML and Razor syntax to provide the UI. The Controller is a class that receives requests and handles user actions.
Use the MVC pattern in your ASP.NET Core applications when you prefer that pattern for building web applications with a clear separation between the model, view, and controller.
Razor Pages was introduced with ASP.NET Core 2.0 and is built on the same infrastructure as ASP.NET Core MVC. Both Razor Pages and MVC can be used within the same ASP.NET Core app. ASP.NET Core MVC is outside of the scope for this module.