liveBook · Manning

UseEndpoints

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

UseEndpoints is an ASP.NET Core application-builder extension method that adds endpoint execution middleware and registers endpoint routes.

UseEndpoints configures endpoint routes and adds the middleware that executes a selected endpoint. It works with UseRouting: UseRouting selects a route by comparing a URL pattern with the request path; UseEndpoints runs the selected endpoint. The separation lets middleware between them inspect the selection before a response is generated. section 13.1.2 section 13.3.3

Listing 13.5 Using the routing middleware in 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.UseRouting();

#pragma warning disable ASP0014

app.UseEndpoints(endpoints => {
    endpoints.MapGet("routing", async context => {
        await context.Response.WriteAsync("Request Was Routed");
    });
});

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

app.Run();

A complete Program.cs example places UseRouting before UseEndpoints and registers a GET endpoint for routing. Requesting http://localhost:5000/routing produces “Request Was Routed.” The pragma suppresses analyzer warning ASP0014. section 13.1.2

UseEndpoints accepts a function whose IEndpointRouteBuilder argument registers routes. Each route pairs a URL pattern with an endpoint. An endpoint can be an asynchronous RequestDelegate that receives HttpContext and writes the response; a middleware component’s Invoke method can also serve as an endpoint. section 13.1.2

Choose the registration method to match the HTTP methods you intend to handle:

  • MapGet, MapPost, MapPut, and MapDelete match their respective HTTP methods and a URL pattern. section 13.1.2
  • MapMethods matches specified HTTP methods; Map matches any HTTP method. section 13.1.2

For simpler Program.cs configuration, register routes directly on WebApplication—for example, app.MapGet("routing", async context => await context.Response.WriteAsync("Request Was Routed"));. WebApplication implements IEndpointRouteBuilder, and ASP.NET Core can apply UseRouting and UseEndpoints automatically. The analyzer recommends this top-level style instead of an explicit UseEndpoints call. Mind the pipeline change: with direct registration, requests are always forwarded, so an ordinary terminal middleware would run even for a matching route. Remove it in this setup; a routing fallback is the alternative discussed for unmatched requests. section 13.1.3

When middleware needs to react to the chosen route, place it between explicit UseRouting and UseEndpoints calls. HttpContext.GetEndpoint() then returns the selected Endpoint, whose DisplayName can identify it. Avoid changing that selection with SetEndpoint unless there is a compelling reason. section 13.3.3

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

How deep it goes
Works through explicit pipeline configuration and contrasts it with direct registration, including middleware-order consequences.
Examples
Tests a routing GET endpoint at http://localhost:5000/routing and maps component methods to capital/uk and population/paris.
What it assumes
Assumes familiarity with C# middleware and HttpContext while defining how URL patterns connect requests to endpoints.
What you can do after
Apply it via ASP.NET Core to register endpoints explicitly or directly on WebApplication and inspect route selection from middleware.
How it connects
Connects UseRouting, IEndpointRouteBuilder, RequestDelegate, HttpContext.GetEndpoint, and terminal middleware.

Also explained in

Compare how 2 books explain UseEndpoints →

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.