MSDN Documentation

ASP.NET Middleware Fundamentals

Middleware is a concept in ASP.NET Core that allows you to build a pipeline of components that can process HTTP requests and responses. Each middleware component has the ability to:

The middleware pipeline is the core of how ASP.NET Core handles requests. It's a series of components that are executed in order for each incoming request.

The Request Pipeline

When an HTTP request arrives at your ASP.NET Core application, it's passed through a series of middleware components. Each component can inspect and modify the request and response.

Key Concept: Think of middleware as a chain of command. Each link in the chain can act on the request before passing it to the next, or it can decide to stop the chain and respond directly.

How Middleware Works

Each middleware component is responsible for a specific task. For example:

A typical ASP.NET Core application configures its middleware pipeline in the Startup.cs file (or Program.cs in .NET 6+ minimal APIs) within the Configure method.

Configuring Middleware

You configure the middleware pipeline using the Use... extension methods on the IApplicationBuilder interface. The order in which you call these methods is crucial as it defines the order of execution.

Example Configuration


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseStaticFiles(); // Serves static files

    app.UseRouting(); // Enables routing

    app.UseAuthentication(); // Handles authentication
    app.UseAuthorization(); // Handles authorization

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
            

Custom Middleware

You can also create your own custom middleware to perform specific logic. A custom middleware component typically has:

Creating a Simple Custom Middleware

Here's an example of a custom middleware that logs the request path:


public class RequestLoggingMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext context)
    {
        Console.WriteLine($"Request received: {context.Request.Path}");
        await _next(context); // Call the next middleware in the pipeline
    }
}
            

To use this middleware, you would add it to the pipeline:


app.UseMiddleware<RequestLoggingMiddleware>();
            

Middleware Ordering

The order matters significantly. For instance, UseRouting must typically come before authentication and authorization middleware, and authentication must come before authorization. Static files middleware usually appears early in the pipeline.

Common Middleware Order

  1. Exception Handling
  2. HTTPS Redirection
  3. Static Files
  4. CORS (if applicable)
  5. Authentication
  6. Authorization
  7. Routing
  8. Endpoint-specific middleware (e.g., MVC, SignalR)

Understanding middleware is fundamental to building robust and efficient ASP.NET Core applications. It provides a flexible and powerful way to handle the entire request processing lifecycle.