Razor Pages combine a page-focused alternative to MVC—each .cshtml file pairs with a PageModel class. Ideal for form-heavy sites without deep controller hierarchies.
File structure
Pages/
Index.cshtml
Index.cshtml.cs // PageModel
Shared/
_Layout.cshtml
PageModel handlers
public class IndexModel : PageModel {
public void OnGet() { }
public IActionResult OnPost() {
if (!ModelState.IsValid) return Page();
return RedirectToPage("/Success");
}
}
Convention: OnGet, OnPost, OnPostAsync map to HTTP verbs.
MVC vs Razor Pages
MVC groups by controller; Razor Pages group by page. Both use Razor syntax and share DI, auth, and EF Core—pick based on team preference and app shape.
Important interview questions and answers
- Q: Can you mix MVC and Razor Pages?
A: Yes in one project—route carefully to avoid conflicts. - Q: When prefer Razor Pages?
A: Page-centric apps, tutorials, internal tools with many similar forms.
Self-check
- Which method handles GET on a Razor Page?
- Where does shared layout live?