Middleware (Http Pipeline) in Asp.net Core | ASP.NET Core Tutorial

What is middleware in asp.net core? Middleware in asp.net Core is the only concept that creates some confusion if you are migrating from asp.net MVC to asp.net core.

Before learning about the middleware in asp.net core, let’s first understand what happens when we send a request from a browser (client) to a server for the asp.net core application.

Dot Net Core application HTTP pipeline

When we send a request from a client to the server (for asp.net core application), we generally assume that the request directly goes to the controller. But before actually hitting the controller the request has to pass through some components. These components are known as HTTP pipeline or middleware.

What is Middleware in asp.net core

A middleware is a piece of code (component) that is used in HTTP pipeline.

  • Asp.net core creates an HTTP application pipeline that processes the request.
  • This HTTP pipeline is configured in the Configuration method of startup.cs
  • All request to the application goes through this HTTP pipeline.
  • We can use multiple middleware in an application (HTTP pipeline).
  • Middleware has access to all the requests and responses.

Since middleware has all the details about the request & response so some of the tasks like logging, user authentication, etc. can be done using middleware.

Execution order of Middleware

In one asp.net core application, we can have n number of middleware. The order of middleware matters a lot.

  • The request travels in middleware one by one in the same order they (middleware) are defined.
  • Each middleware has a next() method. This next() method is used to pass the execution from one middleware to another and so on.
  • If the request finds a middleware without the next() method, then the application starts to generate the response.
  • The response also travels from the last middleware (one without next() method) to the first one by one.

To understand the middleware execution better let’s write some code. Open your asp.net core application and open the startup.cs class.

Navigate to the Configure method and comment on everything that is written in this method (Just for the demo).

We can add a new middleware by using the Use() method. Let’s add our first custom middleware in the asp.net core application.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from first custom middleware");
    });
}

Now run your application and you must see the message (Hello from first custom middleware) in the browser.

Let us add multiple middleware and use the next() method to validate the order of middleware.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from first custom middleware");
        await next();
    });

    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from second custom middleware");
        await next();
    });

    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from third custom middleware");
    });
}


//output
Hello from first custom middleware
Hello from second custom middleware
Hello from third custom middleware

The next() method will pass the execution to the next middleware.

If you will remove the next() the method from the first middleware, then the request will not go to the rest of the two middleware. It will return from the first middleware.

Now to understand the response lets add a few lines of code after the next() method.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from first custom middleware");
        await next();
        await context.Response.WriteAsync("Hello from first custom middleware response");
    });

    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from second custom middleware");
        await next();
        await context.Response.WriteAsync("Hello from second custom middleware response");
    });

    app.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello from third custom middleware");
    });
}


//output
Hello from first custom middleware
Hello from second custom middleware
Hello from third custom middleware
Hello from second custom middleware response
Hello from first custom middleware response
  • All the built-in middleware starts from Use keyword.
    • app.UseAuthentication()
    • app.UseEndpoints()
    • app.UseCors()
    • etc.