For a server-rendered ASP.NET Core app, when would you choose Razor Pages over MVC controllers with views, and what do the two share?
answer
- page-focused versus resource-focused
- dotnet new webapp versus dotnet new mvc
- same binding, validation, tag helpers, layouts
- page filters instead of action filters
- both can live in one app
basics
~20 sChoose Razor Pages when the UI is a set of screens, each owning its GET and POST logic; choose MVC controllers when many actions share a controller or mix views and JSON. Both share MVC's binding, validation, tag helpers and layouts.
solid answer
~40 sRazor Pages (`dotnet new webapp`, `AddRazorPages`, `MapRazorPages`) organises a UI by **page**: each URL is a `.cshtml` file with its `PageModel` beside it, routed by its folder path, with `OnGet` and `OnPost` handlers and `[BindProperty]` input models. MVC (`dotnet new mvc`, `AddControllersWithViews`, `MapControllerRoute`) organises by **controller**: one class groups actions over a resource, views live in `Views/{Controller}`, and routes come from conventions or attributes. Pages suit form-centric screens where each page owns its logic; controllers suit actions that share dependencies or filters, or that return views and JSON side by side. Both sit on the same MVC core: model binding, `ModelState` validation, tag helpers, layouts and partials, view components and most filters, though pages use page filters where controllers use action filters. Neither supports Native AOT, and one app can map both.
go deeper
Recall that Razor Pages organise code by page with a PageModel, while MVC uses controllers and views, and name each template.
Explain what the two share, the routing and handler differences, and the page-filter versus action-filter stage.
Choose per area of an app, mix both where it helps, and recognise limits such as no Native AOT support for either.
Set the UI architecture for a product line: Razor Pages, MVC, Blazor or a split, weighing team skills, migration paths and hosting constraints.
## Two shapes over one framework Razor Pages is not a separate framework: `Microsoft.AspNetCore.Mvc.RazorPages` is part of ASP.NET Core MVC and reuses its machinery. What differs is how code is **organised** and **routed**. | | Razor Pages | MVC controllers with views | |---|---|---| | Template | `dotnet new webapp` | `dotnet new mvc` | | Registration | `AddRazorPages()` + `MapRazorPages()` | `AddControllersWithViews()` + `MapControllerRoute(...)` | | Unit of code | one page: `.cshtml` + `PageModel` | one controller with many actions | | Routing | folder path, plus the `@page` template | conventional route or `[Route]` attributes | | Request methods | handlers `OnGet`, `OnPostAsync`, named handlers | actions with `[HttpGet]`, `[HttpPost]` | | Form input | `[BindProperty]` input models | action parameters | | Views | the page is its own view | `Views/{Controller}/{Action}.cshtml` | | Filter stage around the code | page filters | action filters | ## What they share - **Model binding and validation**: the same binders, `ModelState`, DataAnnotations and client validation attributes. - **Razor views**: layouts, `_ViewStart.cshtml`, `_ViewImports.cshtml`, partials, sections. - **Tag helpers and view components**: `asp-for`, the form helpers, `<vc:...>`. - **Most filters**: authorization, resource, exception and result filters run for both. - **Hosting limits**: both registration methods are marked as not supporting trimming or Native AOT; an AOT app uses Minimal APIs. So a developer who knows one learns the other quickly, and an app can mix them: `AddRazorPages()` alongside `AddControllersWithViews()`, with `MapRazorPages()` and `MapControllerRoute(...)` both mapped. The MVC template does exactly that when authentication is enabled, mapping Razor Pages for the Identity UI. ## When pages fit better A dentist's practice site has a booking page, an appointment list, a details page with cancel and reschedule, and a patient profile. Each screen has its own data and its own form: 1. Each page's GET and POST logic sits in one `PageModel`, next to its markup, so a change touches one folder. 2. Pages carry no shared controller state or dependencies they do not need. 3. URLs follow the folder structure without route configuration. 4. Antiforgery validation is applied to page POSTs automatically. This is why the Razor Pages template is the default `webapp` and why page-per-screen CRUD and form-heavy back offices usually start there. ## When controllers fit better 1. Many actions operate on the same resource and share dependencies, filters or helper methods, so one class keeps them together. 2. Routes do not follow a folder hierarchy, for example several URL shapes leading to one action through attributes. 3. The same controller serves HTML views and JSON for scripts, or the team already has an MVC codebase and conventions. 4. Developers coming from controller-based frameworks expect the action-per-method shape. ## Moving between the two Because the core is shared, moving a screen from a controller to a page is mostly mechanical: 1. The action's view becomes `Pages/<Folder>/<Name>.cshtml` with `@page` at the top and `@model` pointing to a new `PageModel`. 2. The GET action becomes `OnGetAsync`, the POST action `OnPostAsync`; action parameters become handler parameters or a `[BindProperty]` input model. 3. `RedirectToAction` calls become `RedirectToPage` with a page name, and links switch from `asp-controller`/`asp-action` to `asp-page`. 4. Action filters used by that controller are rewritten as page filters or resource and result filters. Layouts, partials, tag helpers, view components and validation attributes carry over unchanged. ## How to answer in an interview A good answer avoids "pages are for simple sites". Both handle large applications; the choice is about cohesion. Pages group code by **screen**, controllers group it by **resource**. Name the shared core (binding, validation, tag helpers, layouts, most filters), the one structural difference in the pipeline (page filters versus action filters), and that both can coexist, so the decision is reversible screen by screen. For a new interactive UI, a Blazor Web App is a separate option with a different rendering model, and a JSON-only API is usually Minimal APIs or `[ApiController]` controllers rather than pages.
- Can Razor Pages and MVC controllers live in the same app, and what does it take?Yes. Register both, `AddRazorPages()` and `AddControllersWithViews()`, and map both, `MapRazorPages()` and `MapControllerRoute(...)`. They share layouts, tag helpers, view components and global filters other than action filters. The ASP.NET Core 10 MVC template does this when authentication is selected, mapping Razor Pages for the Identity UI.
- Why can't either be the basis of a Native AOT web app?`AddRazorPages` and `AddControllersWithViews` are annotated with `RequiresUnreferencedCode`: the framework reflects over pages, controllers, models and views at runtime, which trimming cannot preserve. A Native AOT app uses Minimal APIs with source-generated JSON serialization instead.
MVC is a clinic with one reception desk per department: every request for that department goes through the same desk, which sends the patient to the right room. Razor Pages gives every treatment room its own door and its own receptionist, so a visit to one room never passes through a shared desk.
- Razor Pages is a separate framework that does not use MVC model binding
- Razor Pages only suit small sites, and large apps need controllers
- An app must pick either Razor Pages or controllers, never both
- Razor Pages cannot use layouts, partials or view components
- Razor Pages run in the browser like Blazor WebAssembly