Microsoft Logo

Microsoft Docs

ASP.NET Core MVC & Razor Pages Samples

Explore practical examples and complete project samples to understand and implement ASP.NET Core MVC and Razor Pages effectively.

Core MVC Samples

Basic MVC Application
A foundational example demonstrating the structure and common patterns of an ASP.NET Core MVC application.
Data Binding & Validation
Learn how to bind data to your models and implement robust client-side and server-side validation.
Authentication & Authorization
Implement secure user management, login, registration, and access control in your MVC applications.

Razor Pages Samples

Getting Started with Razor Pages
A simple Razor Page application illustrating the event-driven model and page-centric approach.
Razor Pages with AJAX
Demonstrates how to use AJAX to update parts of a Razor Page without a full page reload.
Form Handling in Razor Pages
Comprehensive examples of creating and submitting forms, including file uploads and complex data structures.

Advanced Topics & Integrations

Integrating with Web APIs
Sample code showing how to consume external Web APIs from both MVC and Razor Pages.
Blazor Interoperability
Examples of how to integrate Blazor components within your ASP.NET Core MVC or Razor Pages applications.

Code Snippet Examples

Routing Configuration
Typical routing setup for MVC and Razor Pages.
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddControllersWithViews(); // For MVC var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); // Default for Razor Pages // Example MVC route mapping (if not using default conventions) app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();