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


     Response

What 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

MiddlewarePurpose
Exception handlingCentralized error handling
HTTPS redirectionRedirect HTTP → HTTPS
AuthenticationEstablish user identity
AuthorizationCheck permissions
RoutingSelect endpoint
Static filesServe 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.