Web applications use a series of components to route each HTTP request to its destination and then return an appropriate response to the user. This series of components is organized in a pipeline which is collectively known as middleware.
The IApplicationBuilder interface defines the built-in middleware methods. These methods are defined by using the format UseX() with X describing the action performed by the method.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
The IApplicationBuilder interface provides a generic Use() method which can be used to process custom middleware components and call the next component in the pipeline. Multiple middleware components can be chained together with the Use() method, which accepts two parameters, the HTTP context and the next middleware component to be called.
app.Use(async (context, next) => {await context.Response.WriteAsync("Custom middleware!");await next();});
IApplicationBuilder.Run() is a terminal middleware component which begins returning the HTTP response. Run() can be added to the end of the request pipeline since any delegates after this component will not be executed.
app.Run(async (context) => {await context.Response.WriteAsync("Terminal middleware!");});
Proper ordering of middleware components is important. The middleware request pipeline has a nested structure where each component can perform operations before and after the next component.

In ASP.NET (Core 8), adding middleware is streamlined using the minimal hosting model. Simply inject middleware components in Program.cs with the convenient builder pattern methods. This flexibility allows for tailored request handling pipelines.
using Microsoft.AspNetCore.Builder;using Microsoft.Extensions.DependencyInjection;var builder = WebApplication.CreateBuilder(args);var app = builder.Build();// Add custom middlewareapp.UseMiddleware<MyCustomMiddleware>();app.Run();
ASP.NET Core 8 handles requests through a middleware pipeline. Each middleware component processes requests in the order registered in Program.cs. For example, logging should be placed early to capture all events.
// Program.csvar builder = WebApplication.CreateBuilder(args);// Register middleware in the desired orderbuilder.Services.AddLogging();var app = builder.Build();// Logging middleware should go firstapp.UseMiddleware<LoggingMiddleware>();app.UseMiddleware<AuthenticationMiddleware>();app.UseMiddleware<AuthorizationMiddleware>();app.UseMiddleware<ResponseCompressionMiddleware>();app.Run();