Microsoft Developer Network
Build modern, cloud-ready, internet-connected applications with ASP.NET Core.
ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. You can develop with ASP.NET Core using C# on Windows, macOS, and Linux.
ASP.NET Core supports various patterns for building web applications, including Model-View-Controller (MVC) and Razor Pages.
The Model-View-Controller (MVC) pattern separates concerns within an application. It's a well-established pattern for building complex web applications.
Razor Pages provide a page-centric model for building web UI. It's simpler than MVC for many common scenarios, especially for form handling.
A typical Razor Page consists of a Razor file (.cshtml
) for the UI and a code-behind file (.cshtml.cs
) for the page logic.
// Example: Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
public class IndexModel : PageModel
{
public string Message { get; set; }
public void OnGet()
{
Message = "Welcome to ASP.NET Core!";
}
}
// Example: Index.cshtml
@page
@model IndexModel
@{
ViewData["Title"] = "Home Page";
}
<h1>@Model.Message</h1>
<p>This is a simple ASP.NET Core Razor Page.</p>
ASP.NET Core makes it easy to build robust Web APIs for your applications.
API controllers are classes that handle HTTP requests and return data, typically in JSON or XML format.
// Example: ProductsController.cs
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using YourApp.Models; // Assuming you have a Product model
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
// In-memory data store for demonstration
private static List<Product> _products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200.00m },
new Product { Id = 2, Name = "Keyboard", Price = 75.00m }
};
[HttpGet]
public IEnumerable<Product> Get()
{
return _products;
}
[HttpGet("{id}")]
public ActionResult<Product> Get(int id)
{
var product = _products.Find(p => p.Id == id);
if (product == null)
{
return NotFound();
}
return product;
}
}
ASP.NET Core uses a middleware pipeline to process HTTP requests. Middleware components are executed in a specific order.
You configure the request pipeline in the Program.cs
file.
// Example: Program.cs
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.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers(); // Map API endpoints
app.Run();
To start building with ASP.NET Core, ensure you have the .NET SDK installed. You can then create a new project using the command line:
dotnet new webapp -o MyWebApp
cd MyWebApp
dotnet run