Each middleware receives the request, can act on it, and either passes it to the next component or short circuits with its own response; on the way back out it can act on the response. Registration order decides behaviour. Exception handling goes first so it can catch everything, static files and routing come early, and authentication must run before authorisation, which must run before the endpoints it protects.
Why interviewers ask this
The pipeline is the backbone of every ASP.NET Core app, and ordering mistakes cause subtle bugs: endpoints that skip authorisation, errors that escape the exception handler, or CORS failures that only appear in the browser. The interviewer wants the request and response flow, short circuiting, and a sensible standard order with reasons. Writing a small custom middleware, and knowing when a filter is the better tool, shows practical experience.
How to structure your answer
- Describe the request and response flow through middleware.
- Explain short circuiting and why registration order matters.
- Give the standard order with a reason for each position.
- Mention custom middleware and when a filter fits better.
Example answer
I picture it as nested layers. A request goes in through each middleware in the order they were registered, and each one can do some work, then call the next, or stop and write a response itself. The response passes back through the same layers in reverse, so a middleware can also act on the way out. So order is behaviour. Exception handling goes first, so it wraps everything else and can turn an unhandled exception into a proper problem details response. HTTPS redirection and static files come early, so a request for a stylesheet short circuits without touching routing or authentication. Then routing, then authentication to establish who the user is, then authorisation to decide whether they can reach the endpoint that routing selected. Swap those last two and authorisation runs with no user. I write custom middleware for cross cutting concerns like adding a correlation id to every request, but if the logic needs to know about controller actions or model binding, an action filter is the better fit.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- Where would you put CORS in the pipeline, and why?
- How would you write middleware that logs how long every request takes?
- What is the difference between middleware and an action filter?
Related .net developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions