05
DecMicrosoft introduced a new feature called Middleware in the ASP.Net Core applications. It is a new concept in the programming language. Basically, Middleware is just a class or component which fired on every request received in the Asp.Net Core Applications. In the previous version of ASP.Net application, there are two different request pipeline exists namely HttpHandlers and HttpModules. Middleware is just similar to like these Http action pipes which can be configured and executed in the both side.
WHAT IS MIDDLEWARE?
In Asp.Net Core, Middleware actually controls how applications behave on the HTTP request. Actually, Middleware are depending upon classes which creates a pipeline which control the request and response flow in the systems. Every section of the middleware can handle either request or response or both. Each section of the middleware also decide whether the related request or response will move to the next section of the pipeline or not. In this way, we can develop a system flow by the help of middleware in the applications which basically handle the request situations which may be not handled properly by the core applications. In the official site of ASP.NET Core, Microsoft provides an excellent image in which they demonstrate the main behavior of the middleware –

So, in simple word, Middleware can :
Generate an HTTP response against the incoming HTTP request.
Can be accept an incoming HTTP request, and then pass that request to the next middleware
Can be populate an HTTP response and pass that response to another middleware or web server.
So every middleware component can execute any specify task before and after the next allotted component in the middleware pipeline is invoked. These request operations deleted can be configured by using Run, Map and Use extensions methods. Any individual or independent request deleted can be executed or mentioned in-line as an anonymous method or it can also be defined as a reusable class. These reusable class or in-line anonymous methods are normally known as middleware components. So, every component in the middleware is responsible for executing the next component in the pipeline.
BUILT-IN MIDDLEWARE
Just like other features, Microsoft introduced so many inbuild middleware with the Asp.Net Components. In this section, we will discuss about some of the build-in middleware.
CONFIGURING MIDDLEWARE
So, if we want to use middleware in our application, then we need to configure middleware in our application. For doing this, we need to use Configure() of Startup class where we can use the instance of IApplicationBuilder. In the below example, we use a single middleware with the help of Run method and it will return “Beginning to Middleware” as a string for each request.
public class Startup { public Startup() { } public void Configure(IApplicationBuilder application, IHostingEnvironment appEnv) { application.Run(async (context) => { await context.Response.WriteAsync("Beginning to Middleware…"); }); } }
In the above code block example, Run() actually is an extension of IApplicationBuilder instance. This extension method basically adds a middleware into the application request pipeline. This method accepts a RequestDelegate parameters which handles any request into the application. The main signature of the Run() is as below –
public static void run (this IApplicationBuilder app, RequestDelegate handler)
Since, this method accepts a RequestDelegate as a parameter, means that it can accept the HttpContext as parameter and it return as Task. That’s why we use lambda expression for returning the string in the above example. We can also specify a separate asynchronous function for returning the result and then that function need to invoke within the Run() as below:-
public void Configure(IApplicationBuilder application, IHostingEnvironment appEnv) { application.Run(FirstMiddleware); } private async Task FirstMiddleware (HttpContext context) { await context.Response.WriteAsync("Beginning to Middleware…"); }
Similar the above example, we can define multiple middleware components within the ASP.NET application which need to be executed sequentially. But for using multiple middleware, we can’t use the Run() since this method add a terminal middleware. For that reason, it can’t be executing the next middleware. In this situation, we need to use Use() extension method for invoke multiple middleware components in our application. The signature of the Use() method is quite similar to the Run() method except that it accept next parameter to execute the next middleware components in the row. The below code demonstrates the same which will execute two middleware components.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { application.Run(async (context,next) => { await context.Response.WriteAsync("Beginning to Middleware…"); }); application.Run(async (context) => { await context.Response.WriteAsync("Beginning Of Middlewar2"); }); }
CREATING A CUSTOM MIDDLEWARE
So, as per the discussions till now, it is clear that middleware basically defines the current step process which need to be executed and also, after completing each step it directed to next step for further execution. So, as per our requirement we need to build multiple middleware in the pipeline of request and write all code within the configure() is not good idea since it will not extensible or clean. We always want to define a middleware as abstract or a separate file so that every middleware can perform a single type of responsibilities. For that purpose, we first create a class named MyMessageMiddleware which will display some text message before and after execution.
public class MyMessageMiddleware { private readonly RequestDelegate _next; public MyMessageMiddleware (RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { await context.Response.WriteAsync("!! Start of Custom Middleware !!\n\r"); await _next(context); await context.Response.WriteAsync("\n\r!! End of Custom Middleware !!\"); } }
In the above class, we use the next RequestDelegate by the help of dependency injection as a part of the constructor. Also, we define a method named InvokeAsync which is as per the convention and we pass HttpContext as parameter to this function. Also, we pass the HttpContext to execute the next delegate call.
CREATING A MIDDLEWARE PIPLELINE
Now, we need to create an extension method of IApplicationBuilder to define the custom middleware pipleline.
public static class MyMessageMiddlewareExtensions { public static IApplicationBuilder UseMyMessageMiddleware(this IApplicationBuilder builder) { return builder.UseMiddleware<MyMessageMiddleware>(); } }
Now, implement this custom middleware extension method in the Startup class:
public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder application, IHostingEnvironment appEnv) { application.UseMyMessageMiddleware (); app.Run(async (context) => { await context.Response.WriteAsync("Beginning to Middleware…"); }); } }
SUMMERY
So in this article, we discuss about the concept of Middleware, why it is required in .net core application, how we can configure it and also how we can create custom middleware for our applications. So, we need to remember the key points related to the middleware:
Middleware actually represents a group of classes which forms a queue or pipeline for handling request or response.
The mentioned pipeline will be end at the first execution of app.Run().
We want to use multiple middleware, then we can use app.Use()
We can create extension methods of IApplicationBuilder to include custom middleware in our application.
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.