ASP.NET Core Middleware
Summary
Middleware is a component in the ASP.NET Core request pipeline that can inspect, modify, short-circuit, or pass a request to the next component.
Mental Model
HTTP Request
│
▼
┌───────────────┐
│ Middleware A │
└───────┬───────┘
▼
┌───────────────┐
│ Middleware B │
└───────┬───────┘
▼
┌───────────────┐
│ Middleware C │
└───────┬───────┘
▼
Endpoint
│
▼
ResponseWhat Middleware Can Do
- Inspect the request
- Modify the request
- Call the next middleware
- Short-circuit the pipeline
- Modify the response
- Handle cross-cutting concerns
Common Middleware
| Middleware | Purpose |
|---|---|
| Exception handling | Centralized error handling |
| HTTPS redirection | Redirect HTTP → HTTPS |
| Authentication | Establish user identity |
| Authorization | Check permissions |
| Routing | Select endpoint |
| Static files | Serve static content |
Interview Trigger
What is middleware in ASP.NET Core?
Short Answer
Middleware is software assembled into the HTTP request pipeline. Each middleware can process the request, invoke the next component, process the response on the way back, or terminate the pipeline early.