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.
Previous
What is ASP.NET Core and how does it differ from ASP.NET Framework?
Next
What is dependency injection in ASP.NET Core?
More C# / .NET Questions
View all →- Intermediate What is ASP.NET Core and how does it differ from ASP.NET Framework?
- Intermediate What is dependency injection in ASP.NET Core?
- Intermediate What is Entity Framework Core?
- Intermediate What is the difference between eager, lazy, and explicit loading in EF Core?
- Intermediate What is the Task Parallel Library (TPL) in C#?