⚙️ C# / .NET Intermediate

What is middleware in ASP.NET Core?

Answer

Middleware in ASP.NET Core are components that form the request processing pipeline. Each middleware component receives an HTTP request, can process it, and either passes it to the next middleware (next()) or short-circuits the pipeline (returning a response directly). Middleware is ordered — order matters. Configured in Program.cs (or Startup.Configure): app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers();. Built-in middleware: authentication, authorization, routing, static files, response caching, exception handling, CORS, session. Custom middleware: app.Use(async (context, next) => { // before; await next(); // after; }); or as a class with InvokeAsync(HttpContext, RequestDelegate). The pipeline is bidirectional — code before next() runs on the way in; code after runs on the way out. Short-circuiting: not calling next() stops pipeline processing.