liveBook · Manning

Middleware

As explained in Building Web APIs with ASP.NET Core

Middleware is an HTTP-level component that processes a request and either passes it to the next pipeline component or produces a response.

Middleware handles work in an ASP.NET Core HTTP request pipeline. Each component can pass the request to the next component or end the pipeline by producing a response. Middleware differs from services: services provide application functionality, while middleware handles requests as they pass through the app. section 1.2.2 section 1.3

Register middleware in Program.cs, the app’s entry point. Middleware configuration commonly uses Use, Map, and Run methods; service registration commonly uses Add methods. A potentially terminal component ends a request only when its rules match. A terminal component always ends it. section 1.2.2

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 starter pipeline enables Swagger in development, adds HTTPS redirection and authorization, maps controllers, then starts the app. section 1.2.2

Give the board-game API a no-cache default

Add custom middleware after app.UseAuthorization() when responses should default to no-cache, no-store. The Use callback sets the cache-control response header, then calls next.Invoke() to continue the pipeline. This is nonblocking middleware: it does not produce the response itself. An endpoint with an explicit ResponseCache directive can override the fallback. section 8.2.2

Listing 8.2 Program.cs file: Custom caching middleware
App.UseAuthorization();
 
app.Use((context, next) =>  #1
{
context.Response.Headers["cache-control"] =
"no-cache, no-store";
return next.Invoke();
});

The callback shows the two responsibilities in order: set a response header, then pass control onward with next.Invoke(). section 8.2.2

To check the fallback, run the API in Debug mode and inspect a response without an explicit caching directive for cache-control: no-cache, no-store. Then check a BoardGames GET response with an explicit directive to confirm it takes precedence. Middleware also handles other pipeline work: JWT bearer authentication adds token reading before authorization. section 8.2.2 section 9.2.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines terminal behavior and walks through registration, a custom Use callback, and response-header checks without exploring pipeline internals.
Examples
Uses a Program.cs starter pipeline, a no-cache callback, response-header checks, and JWT authentication setup.
What it assumes
Introduces middleware alongside services before configuring either in Program.cs, then uses HttpContext and extension methods for custom behavior.
What you can do after
Build it from scratch by adding a Use callback in Program.cs and verifying its cache-control fallback.
How it connects
Connects middleware to services, dependency injection, HTTP response caching, authentication, authorization, and controllers.

Also explained in

Compare how 10 books explain 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.