ASP.NET Core Documentation

Controllers and Razor Pages in ASP.NET Core

ASP.NET Core offers two primary approaches for building web user interfaces: MVC (Model-View-Controller) and Razor Pages. Both are built on the same core ASP.NET Core infrastructure and share many common components like middleware, dependency injection, and configuration. Understanding their differences and when to use each is key to effective web development.

What are Controllers and Views? (MVC)

The MVC pattern separates an application into three interconnected components:

In an MVC application, a request is routed to a specific controller action. The controller then processes the request, possibly interacting with a model, and returns a result, often a view. Views in MVC are typically located in the Views folder, organized by controller name.

Example: A Simple MVC Controller


// Pages/Controllers/HomeController.cs
using Microsoft.AspNetCore.Mvc;

public class HomeController : Controller
{
    public IActionResult Index()
    {
        // Assuming a model here, for simplicity just passing a string
        ViewData["Message"] = "Welcome to the MVC App!";
        return View(); // Renders Views/Home/Index.cshtml
    }

    public IActionResult About()
    {
        ViewData["Title"] = "About Us";
        return View(); // Renders Views/Home/About.cshtml
    }
}
            

Example: Corresponding View (Index.cshtml)


@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>@ViewData["Message"]</p>
</div>
            

What are Razor Pages?

Razor Pages provide a page-centric model that simplifies building web UI with ASP.NET Core. Instead of pairing controllers and views, each Razor Page consists of a single Razor file (.cshtml) and an optional code-behind class (.cshtml.cs) that handles page logic. This approach is ideal for scenarios where you have a distinct UI for each discrete feature or page.

Razor Pages are located in the Pages folder by default. The file path within the Pages folder determines the URL. For example, a file at Pages/About.cshtml would be accessible at the /About URL.

Example: A Simple Razor Page


// Pages/Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;

public class IndexModel : PageModel
{
    public string WelcomeMessage { get; set; }

    public void OnGet()
    {
        WelcomeMessage = "Hello from Razor Pages!";
    }
}
            

Example: Corresponding Razor Page (Index.cshtml)


@page
@model IndexModel
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>@Model.WelcomeMessage</p>
</div>
            

Key Difference: Page-Centric vs. Controller-Centric

MVC is controller-centric, meaning requests are handled by controllers that then orchestrate views. Razor Pages are page-centric, where the page itself (both the markup and its associated code-behind) directly handles the request and renders the UI.

When to Use Which?

Use MVC when:

Use Razor Pages when:

Shared Concepts

Both MVC and Razor Pages in ASP.NET Core benefit from:

It's also possible to mix and match MVC and Razor Pages within the same ASP.NET Core application, although it's generally recommended to stick to one primary pattern for consistency.

Conclusion

Controllers and Razor Pages are both powerful tools in the ASP.NET Core ecosystem. MVC provides a robust, structured pattern for complex applications, while Razor Pages offer a simpler, page-centric approach suitable for a wide range of web UI development tasks. Choosing the right pattern depends on the specific requirements and complexity of your project.