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
HTTPHandlers can be chained in an
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
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:
The error handling should be added to the pipeline as early as possible to handle all errors. Useful middleware:
DeveloperExceptionPageMiddleware- displays stacktrace of an exception
ExceptionHandlerMiddleware- alternative to the above, more suitable for production
StatusCodePagesMiddleware- 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 the
The actual content is generated by various
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.
We can create simple endpoints in middleware with the
Run extension method: