ASP.NET Core & Web API (50 Qs)•Part 1: Request Pipeline, Dependency Injection & Web APIs (1 - 10)
Walk through the ASP.NET Core Request Middleware Pipeline.
senior LevelMost AskedCode ExampleDependency InjectionPipesCI/CD & DevOps
In Simple Words:
The HTTP request pipeline is a sequence of delegate functions (RequestDelegate) arranged in bidirectional order:
- Request arrives at Kestrel web server.
- Passes through middleware components in registered order:
ExceptionHandler -> Hsts -> HttpsRedirection -> StaticFiles -> Routing -> Cors -> Authentication -> Authorization -> Custom Middleware -> Endpoints. - An endpoint handler executes (Controller action or Minimal API route).
- Response traverses back in reverse order through the same middleware components.
csharp
var app = builder.Build();
// Bidirectional Middleware Pipeline:
app.UseExceptionHandler("/error");
app.UseHttpsRedirection();
app.UseAuthentication(); // 1. Who are you?
app.UseAuthorization(); // 2. Are you allowed?
app.MapGet("/api/health", () => Results.Ok(new { status = "healthy", timestamp = DateTime.UtcNow }));
app.Run();