ASP.NET - Custom Middleware Development

Custom middleware is code that runs for every request in an ASP.NET Core application. It sits in the pipeline and can look at the request, change something or decide whether the request should continue. This lets developers add features that apply to the whole application, not just one endpoint.


What Middleware Does
Middleware is activated in the order it is added. When a request enters, each middleware can do work like logging, checking a token or adding headers. It can allow the next middleware to run or stop the request early. This keeps repeated tasks in one place instead of copying them into every endpoint.


How to Create Middleware
A custom middleware is written as a class with a constructor and an InvokeAsync method. The method receives HttpContext, which contains details like path, headers and response. Inside, you can read or change information before the response is sent back.


How Middleware Moves to the Next Step
To continue processing, middleware calls the next delegate. If you don’t call it, the pipeline stops and your middleware sends the final response. This gives control to decide whether to let the request continue or end immediately.


Adding Middleware to the App
After creating the middleware class, it is added using app.UseMiddleware or an extension method. The order matters: middleware placed at the top runs first for all requests, and others run afterward unless the pipeline stops.


Why Middleware Helps
Middleware keeps shared logic in one location so your endpoints stay clean. Things like logging, validation, security and response formatting stay organized and easy to change.


Example

public class LogMiddleware
{
    private readonly RequestDelegate _next;

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

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

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

app.UseMiddleware<LogMiddleware>();

app.MapGet("/", () => "Hello");
app.Run();