IEndpointRouteBuilder registers ASP.NET Core endpoints and associates them with URL patterns. WebApplication implements the interface, so a typical .NET 7 application can call Map* methods directly on app. section 6.2
Endpoint registration can combine different handler types:
- MapGet creates a minimal API endpoint. section 6.2
- MapHealthChecks creates a health-check endpoint. section 6.2
- MapRazorPages registers Razor Pages endpoints. 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();
Shows a small application registering minimal API, health-check, and Razor Pages endpoints with route templates. section 6.2
For advanced scenarios, CreateApplicationBuilder creates a miniature IApplicationBuilder pipeline. Build that pipeline, then pass it to Map with a route template to expose middleware as an endpoint. WebApplication deliberately hides this method, signaling that minimal API endpoints are usually preferable. section 31.1.5
Listing 31.11 An extension method for using the PingPongMiddleware as an endpoint
public static class EndpointRouteBuilderExtensions
{
public static IEndpointConventionBuilder MapPingPong( #1
this IEndpointRouteBuilder endpoints, #1
string route) #2
{
var pipeline = endpoints
.CreateApplicationBuilder() #3
.UseMiddleware<PingPongMiddleware>() #3
.Build(); #3
return endpoints #4
.Map(route, pipeline) #4
.RequireAuthorization(); #5
}
}
Demonstrates an extension method that turns middleware into a reusable mapped endpoint and applies authorization. section 31.1.5
