COMPUTER ENGINEER

Jump to examples
Blog
Next.js / ASP.NET Core / SQL6 min study
15/ 41

The ASP.NET Core pipeline

The middleware pipeline: why order matters

Each middleware can see, change, stop or pass the request on.

UnderstandStudy an examplePractise

Understand the concept

In ASP.NET Core a request passes through middleware components arranged in order. Each receives a ‘next’ delegate: it can do its work and pass the request on, or produce the response itself and short-circuit the pipeline. On the way back the same middleware runs again in reverse order, so one middleware can see both before and after (for example to measure duration).

Order defines behaviour. The exception handler must come first so it can catch exceptions thrown by everything after it. Routing comes before authentication and authorization because authorization must read the selected endpoint’s [Authorize] metadata. Authentication precedes authorization because you cannot decide what is allowed without knowing who is asking.

Wrong order is often a silent bug: if UseAuthorization is written before UseAuthentication, every user looks anonymous and protected endpoints return 401; endpoint-specific rate limit policies registered before routing are not applied. The simulator’s ‘Unauthorised request’ scenario shows step by step why this order works.

WHEN TO USE IT

Security, error handling and performance features only work in the right order.

COMMON PITFALL

Writing UseAuthorization before UseAuthentication or placing the exception handler at the end.

LIVE ANIMATION

A letter’s journey through the city

Think of a request as a letter and the server as a city. Pick a scenario, press play and watch which gates the letter passes and where it gets stopped.

CLIENTEDGEWEB SERVERAPI · ASP.NET COREDATABrowserThe user’s screenNetworkDNS · TCP · TLSReverse proxyTLS termination, compressionNext.js serverSSR and route handlersKestrelHTTP serverException handlerMiddlewareRoutingEndpoint selectionRate limiterMiddlewareAuthenticationWho you areAuthorizationWhat you may doModel bindingHTTP → C# + validationControllerEndpoint codeServiceBusiness rules (DI)RedisDistributed cacheEF CoreLINQ → SQLSQL ServerQuery plan, indexes
OUTBOUND →

Someone asks for admin messages without signing in.

BrowserYour screen. It writes the request and finally turns the answer into a page.
1 / 6
01 / PRACTICE

One topic. Three levels of depth.

Illustrative example
app.UseExceptionHandler();
app.UseRouting();
app.UseRateLimiter();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

The core order used in this project.

This page connects to no real server or database. Code samples are illustrative for .NET 8, Next.js and SQL Server; timings are example values in the simulation, not measurements. Try samples only in your own development environment; never share real connection strings, tokens or passwords.

02 / YOUR TURN

Try it yourself first.

A developer moved app.UseExceptionHandler() below MapControllers() while debugging. How does an exception thrown in a controller behave now?

Show the solution and explanation
Worked solution
Hata yakalayıcı artık controller’dan önce çalışmadığı için istisnayı sarmalamaz.
İstisna sunucunun varsayılan davranışına düşer: biçimsiz bir 500,
geliştirme ortamında ise yığın izi içeren bir sayfa.

A middleware can only wrap what is added after it. The exception handler must come first to be useful.