Razor Pages is a page-based programming model introduced in ASP.NET Core 2.0 to simplify the process of building dynamic, server-side web applications. It’s built on top of the MVC (Model-View-Controller) pattern but provides a more streamlined, page-centric approach—ideal for developers who prefer working with page-focused architectures rather than the traditional controller-action structure.
With Razor Pages, each web page is self-contained, consisting of both the HTML markup and the logic required to handle user input and data. This design makes it easier to build, test, and maintain modern web applications in ASP.NET Core.
What is Razor Pages?
Razor Pages uses the Razor syntax, a lightweight templating engine that combines C# and HTML. Each Razor Page typically has two files:
- A .cshtml file — contains HTML markup and Razor syntax.
- A .cshtml.cs file — known as the PageModel, containing the C# backend logic for handling page events.
This structure allows developers to keep UI and logic separate but closely related, improving organization and readability.
Example. HelloWorld Razor Page
Index.cshtml
Index.cshtml.cs
Here:
- The
@page
directive makes the.cshtml
file a Razor Page. - The
IndexModel
class acts as the code-behind, handling page events. - The
OnGet()
method executes when the page is accessed via an HTTP GET request.
Project Setup for Razor Pages
Creating a Razor Pages project in Visual Studio or via CLI is simple.
Using .NET CLI
This command scaffolds a Razor Pages web application with the required folder structure.
Folder Structure
All Razor Pages reside inside the Pages folder by default, but you can organize them into subfolders for modularity.
How Razor Pages Works
When an HTTP request is made:
- The ASP.NET Core Routing middleware maps the URL to a Razor Page.
- The page’s PageModel executes the appropriate handler method (
OnGet
,OnPost
, etc.). - The .cshtml view is rendered with the model’s data.
- The final HTML response is sent to the browser.
Unlike MVC, there’s no need to define separate controllers or route configurations—the framework handles it automatically.
Handler Methods in Razor Pages
Each Razor Page can respond to different HTTP verbs using page handler methods:
OnGet()
→ Handles HTTP GET requestsOnPost()
→ Handles HTTP POST requestsOnPut()
→ Handles HTTP PUT requestsOnDelete()
→ Handles HTTP DELETE requests
Example. Handling Form Submissions
Contact.cshtml
Contact.cshtml.cs
- The
[BindProperty]
attribute binds form inputs directly to C# properties. OnPost()
executes when the form is submitted.- The view updates dynamically after submission.
Routing in Razor Pages
Routing is automatic in Razor Pages. The path to the .cshtml
file defines the URL route.
Example
Pages/About.cshtml
→/About
Pages/Products/Details.cshtml
→/Products/Details
However, you can customize routes using the @page
directive:
You can also define route parameters:
Details.cshtml.cs
Visiting /products/10
binds 10
to the id
parameter automatically.
Using Model Binding and Validation
Razor Pages simplifies form handling using model binding and data annotations for validation.
Example. Registration Form
Register.cshtml
Register.cshtml.cs
Here:
- The
[Required]
and[EmailAddress]
attributes perform server-side validation. - The
asp-validation-for
tag helper shows validation messages dynamically. - Client-side validation is automatically enabled using jQuery Unobtrusive Validation.
Dependency Injection in Razor Pages
Razor Pages fully supports dependency injection (DI) through the PageModel
constructor.
Example: Injecting a Service
In Program.cs:
This demonstrates how business logic can be separated cleanly from the UI layer.
Layouts and Partial Views
Razor Pages supports Layouts, Partial Views, and Tag Helpers just like MVC.
_Layout.cshtml
defines common site structure (header, footer, navigation).- Partial views like
_LoginPartial.cshtml
can be reused across pages.
Example Layout usage
Razor Syntax Overview
Razor syntax blends C# and HTML seamlessly:
The @
symbol tells the compiler to switch from HTML to C# context, making Razor concise and expressive.
Advantages of Razor Pages
- Simplicity – Easier for beginners compared to MVC.
- Separation of concerns – UI and backend logic are in separate files.
- Better organization – Each page handles its own functionality.
- Lightweight and fast – Less boilerplate code than MVC controllers.
- Built-in security – Works seamlessly with ASP.NET Core Identity and CSRF protection.
- Testability – PageModel classes are easy to test with unit tests.
Real-world Use Cases
Razor Pages is ideal for:
- Content-driven websites (blogs, portfolios, company sites)
- CRUD applications
- Admin dashboards
- Internal business tools
- Prototypes and small-scale SaaS frontends
Conclusion
Razor Pages in ASP.NET Core represents the evolution of web development in .NET — simpler, cleaner, and more maintainable than traditional MVC for many use cases.
It allows developers to focus on page-level functionality, reduces complexity, and integrates seamlessly with other ASP.NET Core features such as dependency injection, routing, model binding, and validation.
Whether you are a beginner or an experienced developer, Razor Pages provides a powerful and efficient way to build fast, secure, and scalable web applications in the modern .NET ecosystem.
Best ASP.NET Core 10.0 Hosting
The feature and reliability are the most important things when choosing a good ASP.NET Core 10.0 hosting. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core , their servers are optimized for PHP web applications such as the latest ASP.NET Core 10.0 version. The performance and the uptime of the ASP.NET Core hosting service are excellent, and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with top equipment like cooling system, fire detection, high-speed Internet connection, and so on. That is why HostForLIFE.eu guarantees 99.9% uptime for ASP.NET Core . And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core hosting are security and always up.