ASP.NET Core Intro, Middleware & Pipeline Masterclass
Welcome to Phase 12 (Chapter 32): ASP.NET Core Introduction, Middleware & Request Pipeline Masterclass! ASP.NET Core is Microsoft's cross-platform, high-performance web framework for building modern web APIs, web applications, and microservices. In this chapter, we cover ASP.NET Core architecture, Program.cs application host setup, the Middleware pipeline, request/response flow, Kestrel web server, development environment setup, and Swagger/OpenAPI documentation.
| Project Type | Purpose | Serves | Common Use Case |
|---|---|---|---|
| Web Application (MVC / Razor Pages) | Server-side rendered HTML | HTML + CSS + JS to browsers | Traditional website, CMS, e-commerce UI |
| Web API (REST) | HTTP data endpoint | JSON / XML responses to any client | Mobile apps, SPAs (Angular/React), IoT devices |
| Blazor (WebAssembly) | C# in browser via WASM | Interactive UI compiled to WebAssembly | Full-stack C# web apps without JavaScript |
var builder = WebApplication.CreateBuilder(args);
// 1. Register services in DI container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// 2. Configure Middleware Pipeline (order matters!)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection(); // Redirects HTTP to HTTPS
app.UseRouting(); // Matches routes to controllers
app.UseAuthentication(); // Validates JWT tokens
app.UseAuthorization(); // Checks permissions/roles
app.MapControllers(); // Maps [Route] controller actions
app.Run(); // Starts Kestrel web server and begins listening
Q1: What is Kestrel?
Kestrel is ASP.NET Core's built-in cross-platform HTTP web server. It handles incoming HTTP connections and is embedded directly in the application process, unlike IIS which was a separate Windows-only host.
Q2: Does middleware order matter?
Yes! Middleware executes in the exact order you register it in Program.cs. Always register UseAuthentication before UseAuthorization, and UseRouting before MapControllers.