Middleware
Last updated
Last updated
Middleware is software that's assembled into an app pipeline to handle requests and responses.
Each component:
Chooses whether to pass the request to the next component in the pipeline.
Can perform work before and after the next component in the pipeline.
The request handling pipeline is composed as a series of middleware components. Each component performs asynchronous operations on an HttpContext
and then either invokes the next middleware in the pipeline or terminates the request.
Example of a middleware class:
Add middleware to pipeline:
When a delegate doesn't pass a request to the next delegate, it's called short-circuiting the request pipeline. Short-circuiting is often desirable because it avoids unnecessary work. For example, Static File Middleware can act as a terminal middleware by processing a request for a static file and short-circuiting the rest of the pipeline.
Do NOT call next.Invoke
after the response has been sent to the client. Changes to HttpResponse
after the response has started throw an exception (like setting headers and a status code).