Endpoint routing separates choosing an endpoint from executing it in the ASP.NET Core middleware pipeline. It supports minimal APIs, Razor Pages, MVC, and other middleware through one routing system. section 6.2 section 14.0
A request passes through two routing stages:
- RoutingMiddleware selects the registered endpoint that matches the request. section 6.2
- EndpointMiddleware executes the selected endpoint, typically near the end of the middleware pipeline. section 6.2
- Middleware placed between these stages can inspect the selected endpoint; middleware placed before routing cannot. section 6.2
Registering several endpoint types
A WebApplication can register a minimal API endpoint with MapGet, a health-check endpoint with MapHealthChecks, and Razor Pages with MapRazorPages. A request whose URL matches a route template reaches the corresponding endpoint; an unmatched URL does not produce a response from endpoint middleware. section 6.2
Listing 6.1 Registering multiple endpoints with WebApplication
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks(); #1
builder.Services.AddRazorPages(); #1
WebApplication app = builder.Build();
app.MapGet("/test", () => "Hello world!"); #2
app.MapHealthChecks("/healthz"); #3
app.MapRazorPages(); #4
app.Run();
Use the listing to compare endpoint registration for minimal APIs, health checks, and Razor Pages in one small application. section 6.2
