Middleware in dot NET Core is a piece of software that is assembled into an application pipeline to handle requests and responses, with each middleware component deciding whether to pass the request along to the next component in the pipeline or short circuit it and return a response directly. The request pipeline works like a chain, where the request flows through each registered middleware in order, such as authentication, routing, and error handling, and then the response flows back out through the same chain in reverse order.
app.UseExceptionHandler("/Error");
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
Real-world example
A web application places its authentication middleware early in the pipeline so that every request is verified before it reaches authorization checks or the actual controller logic, keeping security enforcement consistent across the entire application.
Dependency Injection in .NET Core;What is the difference between Kestrel and IIS?