liveBook · Manning

Request pipeline

As explained in Pro ASP.NET Core 7, Tenth Edition

A request pipeline is an ordered processing path through which an application passes HTTP requests and responses.

An ASP.NET Core request pipeline is an ordered chain of middleware that receives HTTP requests and produces responses. For each request, the platform passes request and response objects through the chain. A component may inspect the request, change the response, or pass control onward. Components can also inspect and modify the response on its return path. If none produces a response, ASP.NET Core returns HTTP 404 Not Found. section 12.2.1 section 12.6

Configure the pipeline in Program.cs after building the application. Service registrations on builder.Services make objects available to the application; middleware registrations on app determine how requests are handled. Registration order deserves attention: changing the sequence can change which component gets a request and whether later components run. section 7.1.5 section 12.0

Three common pipeline decisions are:

  • Serve static content with app.UseStaticFiles(). Endpoint routing is added automatically; app.MapDefaultControllerRoute() registers MVC endpoints using its default route convention. section 7.1.5
  • Add custom middleware with Use or UseMiddleware. It can generate a response, pass the request to the next component, or short-circuit processing. Different URL paths can use different middleware sequences. section 12.0 section 12.6
  • Register supporting services as well as middleware when a feature needs both. Sessions, for example, require a session service and app.UseSession(); the example also registers an in-memory cache. section 16.3.1
Listing 13.3 Replacing the contents of the Program.cs file in the Platform folder
using Platform;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseMiddleware<Population>();
app.UseMiddleware<Capital>();
app.Run(async (context) => {
   await context.Response.WriteAsync("Terminal Middleware Reached");
});

app.Run();

This Program.cs listing makes the order tangible: Population runs before Capital, followed by terminal middleware that writes a response if processing reaches it. section 13.1

Handle a URL or pass it on

A Population middleware checks paths such as /population/london and writes a response when it recognizes one. Otherwise, it passes the request onward. A later Capital middleware handles paths such as /capital/france; the example can be run with dotnet run and checked at http://localhost:5000/population/london and http://localhost:5000/capital/france. For a smaller setup check, TestMiddleware writes product, category, and supplier counts at /test and forwards other paths. section 13.1 section 18.4

The same configuration pattern extends beyond hand-written responses. A pipeline can serve an MVC application or deliver Blazor WebAssembly content under /webassembly. When a request behaves unexpectedly, inspect the registered components, their order, and the endpoint or path they are intended to handle. section 7.1.5 section 37.2.6 section 12.0

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains request and response flow, registration order, short-circuiting, branching, and configuration; develops custom middleware through application examples.
Examples
Uses ordered Population and Capital middleware, browser-checkable URLs, a database-counting test endpoint, and session configuration.
What it assumes
Introduces middleware and services while deferring detailed dependency injection and endpoint routing explanations.
What you can do after
Build it from scratch with Program.cs middleware registrations, then check URL behavior and diagnose ordering or forwarding problems.
How it connects
Connects middleware to services, dependency injection, endpoint routing, MVC, sessions, and Blazor WebAssembly.

Also explained in

Compare how 2 books explain request pipeline →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.