liveBook · Manning

Endpoint middleware

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

Endpoint middleware is the ASP.NET Core middleware component that executes the endpoint selected for an incoming request.

Endpoint middleware is the part of an ASP.NET Core middleware pipeline that runs the endpoint selected for an incoming request. It typically sits at the end of the pipeline, where it invokes a handler and generates a response. That response then travels back through the earlier middleware to the web server. section 4.1 section 6.2

Routing and execution are separate steps. Routing middleware compares the request URL with registered route templates and selects an endpoint. Middleware between routing and endpoint middleware can see that selection before the endpoint runs—for example, to decide whether the request is authorized. If no route matches, endpoint middleware does not generate a response. section 6.2 section 13.1.2

A small minimal API

In a WebApplication, `app.MapGet("/", () => "Hello World!")` defines an endpoint; it does not add middleware. For a GET request to `/`, routing selects that endpoint, and endpoint middleware runs its lambda to produce the response. WebApplication normally adds routing middleware near the start and endpoint middleware at the end, so neither usually needs to be added manually. section 3.7.2 section 4.2.3

Reaching endpoint middleware is not guaranteed. Earlier middleware can return a response without calling the next component. For example, welcome-page middleware placed first can answer a request to `/`, so the mapped `/` endpoint never runs. Middleware order therefore affects which component handles a request. section 4.2.3 section 4.1

Endpoints are not limited to minimal APIs. They can also be Razor Pages, MVC actions, or health checks. With Razor Pages, routing first selects the page; when it runs, a page handler is chosen using the HTTP verb and an optional handler value. In a minimal API, endpoint middleware also interprets the handler’s return type: a string becomes plain text, an ordinary object becomes JSON, and an `IResult` can specify another response. section 6.2 section 15.2 section 5.3

Figure 6.3 Endpoint routing uses a two-step process. The RoutingMiddleware selects which endpoint to execute, and the EndpointMiddleware executes it. If the request URL doesn’t match a route template, the endpoint middleware won’t generate a response.
CH06_F03_Lock3

The diagram separates the moment routing selects an endpoint from the later moment endpoint middleware executes it, including what happens when no route matches. section 6.2

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

How deep it goes
Explains selection, pipeline placement, short-circuiting, and Razor Page handler choice without showing EndpointMiddleware implementation.
Examples
Uses a mapped Hello World endpoint, a welcome page that intercepts it, and Razor Pages with multiple handlers.
What it assumes
Assumes familiarity with HTTP requests and introduces the middleware pipeline through small WebApplication examples.
What you can do after
Operate/configure it through WebApplication endpoint mappings and middleware order, and diagnose why an earlier component prevents endpoint execution.
How it connects
Connects endpoint routing, route templates, minimal APIs, Razor Pages, authorization middleware, and middleware short-circuiting.

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.