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.
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
