ASP.NET Core Request Processing Pipeline

ASP.NET Core Request Processing Pipeline

In this article, I will discuss the ASP.NET Core Request Processing Pipeline with Examples. Please read our previous article before proceeding to this article, where we discussed Middleware Components in ASP.NET Core Applications with Examples. As part of this article, we will discuss the following pointers in detail.

  1. What is the ASP.NET Core Request Processing Pipeline?
  2. Example to Understand ASP.NET Core Request Processing Pipeline
  3. How to create and register multiple middleware components in ASP.NET Core?
  4. What is the execution order of middleware components in the request processing pipeline?
What is the ASP.NET Core Request Processing Pipeline?

The ASP.NET Core Request Processing Pipeline, often called the “Middleware Pipeline,” is a sequence of middleware components that handle an incoming HTTP request and generate an appropriate HTTP response in an ASP.NET Core Web application. The Request Processing Pipeline performs various tasks such as Routing, Authentication, Authorization, Caching, Logging, and many more. Some common middleware components in the ASP.NET Core pipeline include:

  • Routing Middleware: Handles URL routing and maps requests to appropriate controller actions or Razor Pages.
  • Authentication Middleware: Handles user authentication and identity management.
  • Authorization Middleware: Enforces access control rules and permissions.
  • Static Files Middleware: Serves static files (e.g., CSS, JavaScript, images) directly from the file system.
  • CORS Middleware: Enforces Cross-Origin Resource Sharing (CORS) policies.
  • Logging Middleware: Logs information about the request and response.
  • Response Compression Middleware: Compresses responses before sending them to the client.
  • Exception Handling Middleware: Handles unhandled exceptions and provides error responses.
Example to Understand ASP.NET Core Request Processing Pipeline:

To understand the Request Processing Pipeline in ASP.NET Core Web Application, first, let us modify the Main() Method of the Program class as shown below. Here, we are registering three middleware components into the Request Processing Pipeline. As you can see, the first two Middleware Components are registered using the Use() Extension Method so that they have the chance to call the next Middleware Component in the Request Processing Pipeline. The last one is registered using the Run() Extension Method as it will be our terminating components, i.e., it will not call the next component. 

namespace FirstCoreWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var app = builder.Build();

            //Configuring Middleware Component using Use and Run Extension Method

            //First Middleware Component Registered using Use Extension Method
            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Middleware1: Incoming Request\n");
                //Calling the Next Middleware Component
                await next();
                await context.Response.WriteAsync("Middleware1: Outgoing Response\n");
            });

            //Second Middleware Component Registered using Use Extension Method
            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Middleware2: Incoming Request\n");
                //Calling the Next Middleware Component
                await next();
                await context.Response.WriteAsync("Middleware2: Outgoing Response\n");
            });

            //Third Middleware Component Registered using Run Extension Method
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Middleware3: Incoming Request handled and response generated\n");
                //Terminal Middleware Component i.e. cannot call the Next Component
            });

            //This will Start the Application
            app.Run();
        }
    }
}

Now run the application, and you should get the following output in the browser.

ASP.NET Core Request Processing Pipeline with Examples using .NET 6

Understanding ASP.NET Core Request Processing Pipeline Execution Order:

Let us compare the above output with the following diagram to understand the ASP.NET Core Request Processing Pipeline in an easier way.

ASP.NET Core Request Processing Pipeline Execution Order

When the incoming HTTP request arrives, it is first received by the first middleware component, Middleware1, which logs “Middleware1: Incoming Request” in the response stream. As a result, we first see this message on the browser. Once the first middleware logs the information, it calls the next() method, invoking the second middleware in the request processing pipeline, Middleware2.

The second middleware logs the information “Middleware2: Incoming Request”. As a result, we see this log information after the first log. Then, the second middleware calls the next(), invoking the third middleware in the request processing pipeline, Middleware3.

The third middleware handles the request and then produces the response. So, the third information that we see in the browser is “Middleware3: Incoming Request handled and response generated”.

The third middleware component is registered using the Run() Extension Method and is a terminal middleware component. So, from this point, the request pipeline starts reversing. That means the control is returned to the second middleware from this middleware. The second middleware logs the information as “Middleware2: Outgoing Response” and then gives the control back to the first middleware component, and the first middleware component logs the information as “Middleware1: Outgoing Response” as we see in the browser.

Key Points to Remember:
  1. The ASP.NET Core Request Processing Pipeline consists of a sequence of Middleware Components that will be called one after the other.
  2. Each Middleware Component can perform some operations before and after invoking the next Middleware Component using the next method. A middleware component can also decide not to call the next middleware component, which is called short-circuiting the request pipeline.
  3. The Middleware Component in ASP.NET Core has access to both the incoming request and the outgoing response.
  4. The most important point is that the order in which the Middleware Components are added in the Main method of the Program class defines the order in which these Middleware Components will be invoked on requests and the reverse order for the response. So, the order is important for defining the application’s Security, Performance, and Functionality.

In the next article, I will discuss the wwwroot folder in ASP.NET Core Application. In this article, I try to explain the ASP.NET Core Request Processing Pipeline with an example. I hope you enjoy this article. 

3 thoughts on “ASP.NET Core Request Processing Pipeline”

Leave a Reply

Your email address will not be published. Required fields are marked *