Tutorials ASP.NET Core MVC Tutorial
Middleware Pipeline — Complete Guide
Middleware Pipeline — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core MVC Tutorial on Toolliyo Academy.
On this page
ASP.NET Core MVC Tutorial · Lesson 24 of 200
Middleware Pipeline
Getting Started ✓ → Core MVC → Data & Security → Production → Career
Beginner · 3 — Controllers & Views · ~6 min · Section 2: ASP.NET Core Basics & Hosting
What is this?
Middleware is software that runs on every HTTP request in order — like a chain of checkpoints. Examples: force HTTPS, serve CSS files, check login, then route to your controller.
Why should you care?
Auth, logging, and error handling often belong in middleware so you do not copy the same code in every controller.
See it live — copy this example
Create an MVC project (dotnet new mvc), add the code, and run dotnet run.
var app = builder.Build();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Run Example »
Edit the code and click Run — like W3Schools Try it Yourself.
What happened?
- Order matters.
- Static files short-circuit before MVC if the file exists.
- Authentication runs before Authorization.
- MapControllerRoute is typically last among MVC middleware.
Try it yourself
- Open Program.cs and read middleware top to bottom.
- Comment out UseStaticFiles — see CSS disappear.
- Add app.Use(async (ctx, next) => { Console.WriteLine(ctx.Request.Path); await next(); }); and watch terminal on each request.
- Change text or labels in the example and run again — watch the browser update.
- Break the code on purpose (remove a semicolon), read the error message, then fix it.
Remember
Middleware runs in the order written in Program.cs. UseStaticFiles, UseRouting, UseAuth are standard. Custom middleware logs, headers, or exception handling.