liveBook · Manning

Terminal middleware

As explained in Building Web APIs with ASP.NET Core

Terminal middleware is middleware that can end HTTP request processing by returning a response without invoking later pipeline components.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines conditional and unconditional termination, registration order, and the Run convention without implementing custom middleware.
Examples
Uses static-file responses, HTTPS redirects, and a Program.cs pipeline ending with app.Run.
What it assumes
Assumes familiarity with ASP.NET Core middleware while defining the distinction between conditional and always-terminal components.
What you can do after
Configure middleware order and recognize when a component terminates or forwards an HTTP request.
How it connects
Connects terminal middleware with middleware, HTTP request pipelines, static files, HTTPS redirection, authorization, and endpoint mapping.

Also explained in

Compare how 2 books explain terminal middleware →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.