Middleware in ASP.NET Core
The middleware are C# classes or functions that handle HTTP requests and
responses. They are chained together, forming a pipeline, similarly to how
HTTPHandler
s can be chained in an HTTPClient
.
All the requests pass through the middleware, so it’s the right place to handle common cross-cutting concerns. The middleware can process the request/response, optionally modify it and pass on in the pipeline.
Requests/responses travel through the pipeline as HttpContext
objects that can
be modified.
Middleware components should be small and handle just one responsibility.
Middleware can short-circuit the request causing it not to be passed to the middleware parts behind it. If some middleware receives a request, it will also receive a response going back.
Middleware is added to the WebApplication
(.NET 6) or to the
IApplicationBuilder
(prior to .NET 6).
In general, middleware is added like this:
Often, middleware comess with extension methods that are more readable:
Errors
The error handling should be added to the pipeline as early as possible to handle all errors. Useful middleware:
DeveloperExceptionPageMiddleware
- displays stacktrace of an exceptionExceptionHandlerMiddleware
- alternative to the above, more suitable for productionStatusCodePagesMiddleware
- transforms raw error codes into some meaningful error-pages, it can redirect to different pages for different errors. It executes only if response payload is empty, making it possible to use it together with theExceptionHandlerMiddleware
.
EndpointMiddleware
The actual content is generated by various EndpointMiddleware
.
Branching
Normally, middleware pipeline is a single line of components that execute one
after another. We can change that and create branches where some requests go one
way, while others go another way. We use Map
for that.
Middleware Endpoints
We can create simple endpoints in middleware with the Run
extension method: