ASP.NET Core Middleware Pipeline
What common mistakes do candidates make around ASP.NET Core Middleware Pipeline, and how do you avoid them?
Answers use simple, clear English.
Quick interview answer
Common mistakes: Placing UseAuthentication after UseAuthorization; forgetting UseRouting. Best practices: Follow Microsoft template order; use MapWhen for branch pipelines.
Detailed answer
Common mistakes: Placing UseAuthentication after UseAuthorization; forgetting UseRouting. Best practices: Follow Microsoft template order; use MapWhen for branch pipelines. 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: Tech Lead.
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
Only answered follow-ups are shown — click to open with full answers