Terminal middleware ends HTTP request processing by returning a response instead of invoking later pipeline components. It handles endpoint work when its activation conditions are met. section 1.2.1
Keep these pipeline rules in mind:
- Register middleware in order: earlier components run before later ones, so terminal middleware belongs after nonterminal middleware. section 1.2.1
- Distinguish potentially terminal middleware from always-terminal middleware: the former forwards unmatched requests, while the latter always ends the pipeline. section 1.2.1 section 1.2.2
- Treat Run as the always-terminal middleware convention and the last component processed. section 1.2.2
Static files and HTTPS redirection
StaticFileMiddleware terminates a request when the URL identifies an accessible static file. HttpsRedirectionMiddleware terminates non-HTTPS requests with a redirect. If either activation rule fails, the middleware passes the request onward. section 1.2.1
var builder = WebApplication.CreateBuilder(args); #1
// Add services to the container.
builder.Services.AddControllers(); #2
// Learn more about configuring Swagger/OpenAPI
// at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); #2
builder.Services.AddSwaggerGen(); #2
var app = builder.Build(); #3
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger(); #4
app.UseSwaggerUI(); #4
}
app.UseHttpsRedirection(); #4
app.UseAuthorization(); #4
app.MapControllers(); #4
app.Run(); #5
The Program.cs listing shows middleware configuration in sequence, including HTTPS redirection, authorization, controller mapping, and the final app.Run call. section 1.2.2

