Section 5 of 5
Razor Pages vs MVC
🎯 What You'll Learn
- Key differences
- When to use each
- Mixing both approaches
- Migration considerations
Comparison
| Aspect | Razor Pages | MVC |
|---|---|---|
| Organization | Page-focused (1 page = 1 feature) | Controller-focused (1 controller = many actions) |
| Files | .cshtml + .cshtml.cs | Controller.cs + View.cshtml |
| Routing | Convention-based (folder structure) | Attribute or route config |
| Complexity | Simpler, less boilerplate | More complex, more flexible |
| Best For | CRUD, forms, simple pages | Complex apps, APIs, shared logic |
| Learning Curve | Easier | Steeper |
Code Comparison
Razor Pages
Pages/Products/Index.cshtml.cs
C#
public class IndexModel : PageModel
{
public List<Product> Products { get; set; } = new();
public async Task OnGetAsync()
{
Products = await _context.Products.ToListAsync();
}
}
Pages/Products/Index.cshtml
HTML
@page
@model IndexModel
<h1>Products</h1>
@foreach (var p in Model.Products)
{
<p>@p.Name</p>
}
MVC
Controllers/ProductsController.cs
C#
public class ProductsController : Controller
{
public async Task<IActionResult> Index()
{
var products = await _context.Products.ToListAsync();
return View(products);
}
}
Views/Products/Index.cshtml
HTML
@model List<Product>
<h1>Products</h1>
@foreach (var p in Model)
{
<p>@p.Name</p>
}
When to Use Razor Pages
✅ Choose Razor Pages For:
- Simple CRUD apps: Product catalog, blog, admin panel
- Form-heavy apps: Contact forms, surveys, registration
- Page-focused scenarios: Each page is independent
- Beginners: Easier to learn and understand
- Small to medium apps: Less boilerplate code
- Rapid prototyping: Faster development
When to Use MVC
✅ Choose MVC For:
- RESTful APIs: Use Web API controllers
- Complex routing: Custom routing patterns
- Shared logic: Multiple actions with common code
- Large enterprise apps: Better separation of concerns
- Existing MVC apps: Maintain consistency
- Team preference: Team familiar with MVC pattern
Mixing Both Approaches
You can use both Razor Pages and MVC in the same application!
Program.cs
C#
var builder = WebApplication.CreateBuilder(args);
// Add both services
builder.Services.AddControllersWithViews(); // MVC
builder.Services.AddRazorPages(); // Razor Pages
var app = builder.Build();
// Map both endpoints
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
Example Use Case
- Razor Pages: Admin panel, user profile pages
- MVC: Public-facing pages, API endpoints
Migration Considerations
MVC to Razor Pages
| MVC | Razor Pages |
|---|---|
| Controller action | Handler method (OnGet/OnPost) |
| View(model) | Page() with properties |
| RedirectToAction | RedirectToPage |
| asp-controller/asp-action | asp-page |
Best Practices
- Choose based on needs: Not one-size-fits-all
- Consistency: Stick to one approach per feature area
- Start simple: Use Razor Pages, migrate to MVC if needed
- Mix when beneficial: Use both if it makes sense
- Team decision: Consider team expertise
Key Takeaways
- Razor Pages: Simpler, page-focused, great for CRUD
- MVC: More complex, controller-focused, better for large apps
- Both are valid: Choose based on your needs
- Can mix: Use both in the same application
- Razor Pages = easier learning curve
- MVC = more flexibility
🎉 Part XI Complete!
Congratulations! You've completed Part XI: Razor Pages. You now understand:
- ✅ What Razor Pages are and when to use them
- ✅ Page Model pattern with properties and binding
- ✅ Handler methods (OnGet, OnPost, named handlers)
- ✅ Forms and validation with data annotations
- ✅ Razor Pages vs MVC comparison
You now have the skills to build page-focused web applications with Razor Pages! 🚀