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

