loading experience

ASP.NET Core

Razor Pages: when they are the right choice

A simple model, one page and its code: why Razor Pages remains excellent for websites and business apps.

Razor Pages: when they are the right choice

In ASP.NET Core you can build web interfaces with MVC, Razor Pages or Blazor. Razor Pages is often underrated, yet it is the easiest model to maintain for many real projects.

The redirect after saving avoids duplicate form submissions.
The redirect after saving avoids duplicate form submissions.

One page, one model

Each page has a .cshtml file for the markup and a PageModel class with the request handlers. Everything about a screen lives in one place.

public class ContactModel(IEmailService email) : PageModel
{
    [BindProperty]
    public ContactRequest Input { get; set; } = new();

    public void OnGet() { }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid) return Page();
        await email.SendAsync(Input);
        TempData["Message"] = "Thank you, we will reply soon.";
        return RedirectToPage();
    }
}

Why it works well

  • File-based routing: the folder structure is the URL structure.
  • Secure by default: antiforgery protection is active on forms with no configuration.
  • Natural Post-Redirect-Get pattern, which avoids double form submissions.
  • Server-side HTML: great for SEO, accessibility and slow devices.

Multiple handlers on the same page

public async Task<IActionResult> OnPostDeleteAsync(int id) { /* ... */ return RedirectToPage(); }
public async Task<IActionResult> OnPostPublishAsync(int id) { /* ... */ return RedirectToPage(); }
<form method="post" asp-page-handler="Delete" asp-route-id="@item.Id">
    <button type="submit">Delete</button>
</form>

When to prefer something else

If you need highly interactive interfaces with continuous updates without reloading the page, Blazor is a better fit. If you only build APIs for a mobile app or a separate frontend, Minimal APIs are leaner. But for corporate websites, members' areas and business applications, Razor Pages is hard to beat for simplicity.

Comments (0)

No comments yet.

Leave a comment