---
title: "What are Razor Pages in .NET Core?"  
description: "What are Razor Pages in .NET Core?"  
author: "Sanjay Goenka"  
published: 2023-03-06  
updated: 2023-06-18  
canonical: https://www.mindstick.com/forum/157469/what-are-razor-pages-in-dot-net-core  
category: "asp.net core"  
tags: ["asp.net", "asp.net core"]  
reading_time: 5 minutes  

---

# What are Razor Pages in .NET Core?

**What are [Razor Pages](https://www.mindstick.com/forum/158607/explain-the-concept-of-razor-pages-and-how-they-are-used-in-asp-dot-net-core) in .NET Core?**

## Replies

### Reply by Aryan Kumar

Razor Pages are a new feature in .NET Core that allows you to create web pages that combine server-side code with HTML. Razor Pages are a simpler and more productive way to create web pages than traditional MVC views.

Razor Pages are based on the Razor syntax, which is a lightweight markup language that allows you to embed server-side code in HTML. Razor Pages files have the .cshtml extension.

The following is an example of a Razor Page:

HTML

```plaintext
@page
@model RazorPagesDemo.Models.Product

<!DOCTYPE html>
<html>
<head>
    <title>Razor Page Demo</title>
</head>
<body>
    <h1>Product Details</h1>
    <p>Product Name: @Model.Name</p>
    <p>Product Price: @Model.Price</p>
</body>
</html>
```

This Razor Page displays the name and price of a product. The `@page` directive tells the ASP.NET Core runtime that this is a Razor Page. The `@model` directive tells the ASP.NET Core runtime that the model for this Razor Page is a `Product` object.

The `Product` object is a simple class that has two properties: Name and Price. The `Name` property is a string, and the `Price` property is a decimal number.

The `h1` and `p` elements in the Razor Page are HTML elements. The `@Model.Name` and `@Model.Price` expressions are Razor expressions that evaluate to the value of the `Name` and `Price` properties of the `Product` object.

Razor Pages are a powerful and versatile way to create web pages. They are easy to learn and use, and they offer a number of advantages over traditional MVC views.

Here are some of the benefits of using Razor Pages:

- They are easier to learn and use than traditional MVC views.
- They are more productive because you can embed server-side code in HTML.
- They are more maintainable because the code and the markup are in the same file.
- They are more extensible because you can use Razor extensions to add new features.

If you are developing an ASP.NET Core application, you should consider using Razor Pages. They are a great way to create web pages that are easy to maintain and extend.

### Reply by Ravi Vishwakarma

**[Razor](https://www.mindstick.com/articles/12002/asp-dot-net-mvc5-razor-with-jquery) Pages** is a new feature introduced in ASP.NET Core 2.0 that makes it easier to build web pages with clean separation of concerns between the UI and application logic. It is a lightweight and streamlined web framework designed for building web applications with minimal ceremony and configuration.

Razor Pages use the Razor view engine to render HTML markup and C# code to generate dynamic content. They are designed to be more focused and easier to understand than the traditional ASP.NET MVC framework, which can be overwhelming for beginners. Razor Pages are based on the Model-View-Controller (MVC) design pattern, but they do not require controllers. Instead, each Razor Page has its own PageModel class, which contains the logic and data for that page.

Razor Pages are ideal for building small to medium-sized web applications that do not require complex routing, middleware, or authentication. They are also great for rapid prototyping and building proof-of-concept applications. However, if you need more advanced features such as API integration, authentication, or complex routing, you may want to consider using ASP.NET Core MVC instead.

Overall, Razor Pages provide a simple and intuitive way to build web applications with ASP.NET Core, without sacrificing the power and flexibility of the framework.

**Suppose you want to create a simple web page that displays a list of products from a database. You can create a Razor Page for this using the following steps:**

- Create a new ASP.NET Core project in Visual Studio or your preferred IDE.
- Right-click on the project in Solution Explorer and select "Add" > "New Folder" to create a new folder for your Razor Pages.
- Right-click on the new folder and select "Add" > "New Item" to create a new Razor Page. Name it "Products.cshtml".
- In the "Products.cshtml" file, add the following code to create a basic HTML structure for the page:

```cs
<!DOCTYPE html>
<html>
<head>
    <title>Products</title>
</head>
<body>
    <h1>Products</h1>
    <ul>
        @foreach (var product in Model.Products)
        {
            <li>@product.Name - @product.Price</li>
        }
    </ul>
</body>
</html>
```

- In the same folder as the "Products.cshtml" file, create a new file named "Products.cshtml.cs". This will be the code-behind file for the Razor Page.
- In the "Products.cshtml.cs" file, add the following code to create a PageModel class:

```cs
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;

namespace RazorPagesExample.Pages
{
    public class ProductsModel : PageModel
    {
        public List<Product> Products { get; set; }

        public void OnGet()
        {
            Products = new List<Product>
            {
                new Product { Name = "Product 1", Price = 10.99m },
                new Product { Name = "Product 2", Price = 19.99m },
                new Product { Name = "Product 3", Price = 8.99m }
            };
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
}
```

- In the "OnGet" method of the PageModel class, we are creating a list of Product objects and setting it to the "Products" property of the class.
- Run the application, and navigate to the "Products" page. You should see a list of products displayed on the page.

This is just a simple example, but it demonstrates the basic structure and functionality of a Razor Page in ASP.NET Core. In practice, you can use Razor Pages to build more complex web applications with multiple pages and advanced features like forms, authentication, and data validation.


---

Original Source: https://www.mindstick.com/forum/157469/what-are-razor-pages-in-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
