ASP.NET Core  

Three Ways to Define Middleware in .NET Core (with Examples)

Introduction

Middleware in .NET Core is a powerful concept that allows developers to intercept, process, and respond to HTTP requests within a customizable pipeline. Each middleware component can perform actions both before and after the next component executes — making it an essential part of request processing.

This article explores three common ways to define middleware in .NET Core, complete with examples and usage scenarios:

  1. Inline Middleware

  2. Custom Middleware

  3. Extension Method Middleware

Inline Middleware

Inline middleware is the simplest and most direct way to define middleware. It’s ideal for quick logic, prototypes, or lightweight request processing.

Example. Logging Request and Response

  
    public void Configure(IApplicationBuilder app)
{
    app.Use(async (context, next) =>
    {
        Console.WriteLine("Request: " + context.Request.Path);
        await next.Invoke();
        Console.WriteLine("Response: " + context.Response.StatusCode);
    });

    app.Run(async context =>
    {
        await context.Response.WriteAsync("Hello from inline middleware!");
    });
}
  

Use Case

• Quick logging
• Diagnostics
• Simple request filtering

Custom Middleware

A custom middleware is a more modular and reusable approach. You define a separate class that encapsulates the logic and can be used across multiple projects.

Example. Logging Middleware

public class LoggingMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext context)
    {
        Console.WriteLine("Logging request: " + context.Request.Path);
        await _next(context);
        Console.WriteLine("Logging response: " + context.Response.StatusCode);
    }
}

Register in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<LoggingMiddleware>();
}

Use Case

Complex logic

  • Reusable components

  • Better separation of concerns

Extension Method Middleware

The extension method middleware approach encapsulates middleware registration logic into a clean, reusable extension method. This keeps your Startup or Program class neat and readable.

Example. Logging Middleware Extension

public static class LoggingMiddlewareExtensions
{
    public static IApplicationBuilder UseLogging(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<LoggingMiddleware>();
    }
}

Use in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseLogging();
}

Use Case

  • Clean and fluent middleware registration

  • Consistent usage across projects

  • Simplifies pipeline configuration

Summary Table

MethodFlexibilityReusabilityBest For
Inline MiddlewareLowNoneQuick tasks, debugging, or prototyping
Custom MiddlewareHighHighModular, testable components
Extension Method MiddlewareMediumHighClean registration, reusable APIs

Key Takeaway

Middleware in .NET Core is composable, meaning you can chain multiple components together to build powerful pipelines. Start with inline middleware for quick experiments, and evolve to custom and extension-based middleware for cleaner, maintainable architectures.

We learned the new technique and evolved together.

Happy coding!