ASP.NET Core Middleware Pipeline
What are the key trade-offs of ASP.NET Core Middleware Pipeline? When would you choose it vs alternatives?
Answers use simple, clear English.
Quick interview answer
Pros: Composable cross-cutting pipeline; same model for minimal APIs and MVC. Cons: Wrong order breaks auth/CORS; async middleware must await next().
Detailed answer
Pros: Composable cross-cutting pipeline; same model for minimal APIs and MVC. Cons: Wrong order breaks auth/CORS; async middleware must await next(). Decision: pick when benefits outweigh operational cost. Core: Request flows through middleware chain: Exception → HTTPS → Routing → Auth → Endpoints. Each can short-circuit or call next(). Order matters critically. Real-time example: Custom correlation ID middleware adds X-Request-Id header before logging middleware runs. Pros: Composable cross-cutting pipeline; same model for minimal APIs and MVC. Cons: Wrong order breaks auth/CORS; async middleware must await next(). Common mistakes: Placing UseAuthentication after UseAuthorization; forgetting UseRouting. Best practices: Follow Microsoft template order; use MapWhen for branch pipelines. Audience level: Junior.
Full explanation
Request flows through middleware chain: Exception → HTTPS → Routing → Auth → Endpoints. Each can short-circuit or call next(). Order matters critically.
Real example & use case
Custom correlation ID middleware adds X-Request-Id header before logging middleware runs.
Pros & cons
Pros: Composable cross-cutting pipeline; same model for minimal APIs and MVC. Cons: Wrong order breaks auth/CORS; async middleware must await next().
Common mistakes
Placing UseAuthentication after UseAuthorization; forgetting UseRouting.
Best practices
Follow Microsoft template order; use MapWhen for branch pipelines.
Follow-up questions
Open one as its own read / solve / listen card