Roadmap › ASP.NET Core MVC (and Razor Pages) › Module: Filters, State and Security
Lessons in this course (15)
15. Razor Pages vs MVC
BasicMediumLesson 15 of 15
Razor Pages (2.0+) organises UI by page rather than by controller: Pages/Products/Edit.cshtml + Edit.cshtml.cs (PageModel with OnGet/OnPostAsync handlers, [BindProperty]). It's simpler for form-centric CRUD pages; MVC suits apps with many actions per resource, complex routing or shared controllers with APIs. Both use the same Razor, tag helpers, model binding, validation, filters and DI, and can live in one app.
csharp
public class EditModel(AppDbContext db) : PageModel
{
[BindProperty] public ProductInput Input { get; set; } = new();
public async Task<IActionResult> OnGetAsync(int id) { /* load */ return Page(); }
public async Task<IActionResult> OnPostAsync(int id)
{
if (!ModelState.IsValid) return Page();
await db.SaveChangesAsync();
return RedirectToPage("Index");
}
}