How is middleware configured?
Ask by Anubhav Sharma
Updated 19 May 2025
Middleware in ASP.NET Core is configured by adding it to the HTTP request pipeline inside the
Startup.Configuremethod (or inProgram.csfor .NET 6 and later). This pipeline is built using theIApplicationBuilderinterface with a series of method calls likeUseMiddleware(),UseRouting(),UseAuthentication(), and so on.How to Configure Middleware — Step by Step
1. Inside
Startup.Configure(Pre .NET 6 style).UseXyz()methods to add middleware that passes control to the next component..Run()can be used to terminate the pipeline early (not common except for very specific cases).2. In
Program.cs(Minimal Hosting Model — .NET 6 and later)3. Adding Custom Middleware
You can add your own middleware with:
Or create a class middleware and add it with:
Summary
IApplicationBuilderinside theConfiguremethod or the minimal hostingProgram.cs..Use...()for middleware that calls the next one,.Run()for terminal middleware.