In this article, let's learn about different types of
Middleware in ASP.NET.
Note: If you have not done so already, I recommend you read the article on Introducing Middleware in ASP.NET.
Table of Contents
- Run Middleware
- Use Middleware
- UseWhen Middleware
- Map Middleware
- MapWhen Middleware
Middleware's are C# classes and logics connected by chain of responsibility pattern to handle and terminate request pipeline in the order of appearance. Middleware's are configured by default in Startup and the order in which they run are important. Request's are processed from top to bottom and outside to inside and back again. These are created once in Startup.
There are 5 types of middleware:
Run Middleware execute the delegate and terminates processing the request pipeline. Run delegates don't
receive a next parameter. The first Run delegate is always terminal and terminates the pipeline. Run should always be the last one in pipeline.
The simplest possible ASP.NET Core app sets up a single request delegate that handles all requests. This case doesn't include an actual request pipeline. Instead, a single anonymous function is called in response to every HTTP request.
Code Sample - Run Middleware
Use Middleware execute the delegate and proceed to next in request pipeline. We can chain multiple request
delegates together with Use. The
next parameter represents the next delegate in the pipeline. You can
short-circuit the pipeline by not calling the next parameter. You can typically perform actions both before and after the next delegate.
Code Sample - Use Middleware
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.
UseWhen Middleware branches the request pipeline based on the result of the given predicate and this branch is
rejoined to the main pipeline if it doesn't short-circuit or contain a terminal middleware.
Code Sample - UseWhen Middleware
Map Middleware conditionally execute a method and does not return to request pipeline. Map extensions
are used as a convention for branching the pipeline. Map branches the request pipeline based on matches of the given request path. If the request
path starts with the given path, the branch is executed.
Code Sample - Map Middleware
MapWhen branches the request pipeline based on the result of the given predicate. Any predicate of type
Func<HttpContext, bool> can be used to map requests to a new branch of the pipeline. Unlike with
UseWhen, this branch is not rejoined to the main pipeline.
Code Sample - MapWhen Middleware
In this article, we learn't different types
Middleware in ASP.NET and how it processes the request pipeline
and how we can use middleware to customise the HTTP request.