In this article, I will explain middleware concepts as follows
- What is middleware
- Middleware Order of Execution
- Create middleware using IApplicationBuilder
- Run, Use, and Map method
- Custom middleware
- Built-in middleware
What is middleware?
Middleware is just a simple class that consists of encapsulated information into an application pipeline to deal with the HTTP request-response pipeline.
Every part of this class has the option to pick whether to give the request to the next pipeline and can play out specific activities when the following request passes in the pipeline.
In this case, Request delegates are utilized to construct the request pipeline.
The request delegates handle every HTTP request.
Request delegates are designed to configure the Run, Map, and Use method on the IApplicationBuilder type that is passed into the Configure method in the Startup class. An individual request delegate can be determined in-line as a lambda expression, or it can be designed in a reusable class.
These reusable classes are middleware or middleware parts. Each middleware part in the request pipeline is liable for invoking the next pipeline or shortcircuiting the request.
Every component of middleware plays a specific role, we can mix up the role but this is not a good approach in software-designed architecture.
So we should create for the specific purpose of single middleware.
For example, we see how Microsoft created so much middleware for specific problems.
I will explain in just a bit about built-in middleware here.
Middleware Order of Execution
The middleware Order of Execution plays a very important role so you have to take care of it very precisely, otherwise you may get unexpected behavior.
So suppose you have 3 middleware - M1, M2, and M3 in sequential order, then it will invoke in the same order and then respond in the reverse direction.
Let's take another example here.
app.UseAuthentication();
app.UseAuthorization();
Here we have set first authentication then authorization, so the order matters, because in real projects we do first authentication, then authentication.
Please refer to this image as it is taken from MS doc reference.

Create middleware using IApplicationBuilder
We can create middleware in the Configure method of the Startup class using the IApplicationBuilder interface.
Here in this following example, I will add a simple middleware using the Run method that will return a string "Hello my first middleware," on every request.
public class Startup {
public Startup() {}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
// here i have configure middleware using IApplicationBuilder with Run merhod.
// I will explain Run method in details just a bil.
app.Run(async (context) => {
await context.Response.WriteAsync("Hello my first middleware");
});
// I have removed hereother code for the clarity..
}
}
In this example, I have used the Run() method. This is an extension method of the IApplicationBuilder interface.
Here I have created middleware using lambda expression, but we can also create it using a private method like this.
public class Startup {
public Startup() {}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.Run(TestMiddleware);
}
private Task TestMiddleware(HttpContext context) {
return context.Response.WriteAsync("Hello TestMiddleware");
}
}
Run, Use, and Map method
The run () method behaves like a terminal middleware delegate to the application's request pipeline.
It will not invoke the next one in the pipeline.
For example


sachin dubeyPosted Jul 23, 2020, 12:58 AM
Good hamid..Very informative and nice article.. keep it up.. proud of you !
Roshan RathodPosted Jul 23, 2020, 12:41 AM
Great nice and clean content ..keep it up