ASP.NET Core  

Understanding ASP.NET Core Middleware: The Heart of the Request Pipeline

Introduction

ASP.NET Core is known for its high performance, modularity, and flexibility, and one of the key building blocks behind these strengths is Middleware.

Middleware forms the request–response pipeline in ASP.NET Core applications. Every HTTP request and response flows through this pipeline, making middleware a critical concept for any .NET Core developer.

In this article, we will explore:

  • What middleware is

  • How the ASP.NET Core request pipeline works

  • Built-in middleware

  • Custom middleware creation

  • Best practices

What Is Middleware in ASP.NET Core?

Middleware is a software component that:

  • Handles HTTP requests and responses

  • Can inspect, modify, or short-circuit requests

  • Is executed sequentially in a pipeline

Each middleware component can:

  1. Process the request

  2. Call the next middleware

  3. Process the response

Think of middleware as checkpoints that every request must pass through.

ASP.NET Core Request Pipeline

When a request hits an ASP.NET Core application:

  1. It enters the middleware pipeline

  2. Each middleware runs in the order it is registered

  3. The response flows back in reverse order

Request → Middleware 1 → Middleware 2 → Middleware 3 → Controller
Response ← Middleware 1 ← Middleware 2 ← Middleware 3

The order of middleware registration is extremely important.

Common Built-in Middleware

ASP.NET Core provides several built-in middleware components:

MiddlewarePurpose
UseRoutingMatches incoming requests to routes
UseAuthenticationAuthenticates users
UseAuthorizationAuthorizes users
UseEndpointsExecutes matched endpoints
UseExceptionHandlerGlobal exception handling
UseStaticFilesServes static files

Example pipeline in Program.cs:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseExceptionHandler("/error");
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.Run();

Types of Middleware

1. Inline Middleware

Defined directly using Use or Run.

app.Use(async (context, next) =>
{
    // Before next middleware
    await context.Response.WriteAsync("Before Middleware\n");
    
    await next();
    
    // After next middleware
    await context.Response.WriteAsync("After Middleware\n");
});

2. Terminal Middleware

Does not call the next middleware.

app.Run(async context =>
{
    await context.Response.WriteAsync("Request handled here.");
});

Once Run() is executed, the pipeline stops.

3. Custom Middleware

For reusable and clean code, create custom middleware.

Step 1: Create Middleware Class

public class RequestLoggingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestLoggingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        Console.WriteLine($"Request: {context.Request.Method} {context.Request.Path}");
        await _next(context);
    }
}

Step 2: Register Middleware

app.UseMiddleware<RequestLoggingMiddleware>();

Short-Circuiting the Pipeline

Middleware can stop further execution:

app.Use(async (context, next) =>
{
    if (!context.User.Identity.IsAuthenticated)
    {
        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
        return;
    }

    await next();
});

This technique is commonly used in:

  • Authentication

  • Authorization

  • Rate limiting

Best Practices for Middleware

Register middleware in the correct order
Keep middleware lightweight
Use custom middleware for reusable logic
Avoid heavy business logic in middleware
Prefer exception-handling middleware at the top

Don’t perform long-running tasks
Don’t ignore async/await

Middleware vs Filters

MiddlewareFilters
Works on HTTP levelWorks on MVC level
Applies globallyApplies to controllers/actions
Runs before routingRuns after routing

Use middleware for cross-cutting concerns like logging, security, and error handling.

Conclusion

Middleware is the backbone of ASP.NET Core. Understanding how it works empowers developers to:

  • Build secure applications

  • Improve performance

  • Implement clean cross-cutting concerns

Mastering middleware means mastering the ASP.NET Core request pipeline.