liveBook · Manning

RequestDelegate

As explained in ASP.NET Core in Action, 3e

RequestDelegate is an ASP.NET Core delegate type representing a function that processes an HTTP context and returns asynchronously.

RequestDelegate represents the next step in an ASP.NET Core HTTP pipeline: a function that receives an HttpContext and returns a Task. Middleware stores one in its constructor and invokes it when the current component should pass the request onward. section 31.1.4 section 31.4

Use the delegate to control whether middleware handles a request or continues the pipeline:

  • Call _next.Invoke(context), or simply _next(context), when another middleware should handle the request. section 31.4

A middleware branch

A health-check middleware returns plain-text "pong" for requests beginning with /ping. For every other path, it invokes the supplied RequestDelegate, leaving the next pipeline component responsible for the response. section 36.1

Because RequestDelegate is a one-parameter function, tests can supply a small lambda that records whether the next component ran. This makes it possible to verify both delegation and response-producing branches.

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines the HttpContext-to-Task contract and demonstrates invocation, testing, and middleware encapsulation without detailing delegate internals.
Examples
Uses headers middleware, security.txt handling, and health-check middleware with matching and nonmatching paths.
What it assumes
Assumes basic C# functions and asynchronous Tasks while establishing the middleware and HttpContext relationship.
What you can do after
Build custom middleware that handles selected requests or forwards them through the pipeline, then test both outcomes.
How it connects
Connects middleware, HttpContext, dependency injection, endpoint routing, and unit testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.