In classic ASP.NET, you might have touched HttpModule/HttpHandler for certain cross‑cutting needs. ASP.NET Core replaces them with Middleware and embraces a pipeline model — you’ll use middleware a lot.
What is middleware?
In Startup.Configure
you compose the pipeline:
|
|
Each Use...
adds a middleware. UseStaticFiles
serves static files; UseMvc
enables MVC routing. Remove UseMvc
and controller routes won’t work.
Differences from HttpModule
HttpModule used a fixed set of lifecycle events; you had to choose carefully where to plug in. Middleware is just code you write that receives HttpContext
, can act, and then choose to call the next middleware.
Flow
The engine calls the first middleware’s Invoke(HttpContext)
, which can do work then call the next middleware, and so on. On the way back up the stack, you can also perform post‑processing.
Write a middleware
|
|
Register it:
|
|
Order matters
Place UseSampleMiddleware
before UseMvc
so redirects happen before MVC routing. If you put it before UseStaticFiles
and try to redirect to a static page, it will fail because static file serving hasn’t been added yet.
Internals (high level)
RequestDelegate
represents a middleware delegate. IApplicationBuilder.Use(...)
chains delegates into a stack; UseMiddleware<T>
validates your type (has a single Invoke(HttpContext)
) and builds the delegate. When the host starts, the pipeline is built once (Build/Run) and reused per request.