liveBook · Manning

IEndpointRouteBuilder

As explained in ASP.NET Core in Action, 3e

IEndpointRouteBuilder is the ASP.NET Core interface for registering route endpoints and creating endpoint-specific application builders.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains direct mapping and advanced middleware conversion, but defers broader route-template details and endpoint implementation internals.
Examples
Uses /test, /healthz, /ping, MapGet, MapHealthChecks, MapRazorPages, and a PingPongMiddleware endpoint.
What it assumes
Assumes basic ASP.NET Core application setup and introduces endpoint handlers, route templates, and endpoint middleware relationships.
What you can do after
Operate/configure endpoint registration directly on WebApplication or build reusable middleware-backed mapping extensions.
How it connects
Connects endpoint routing with WebApplication, route templates, minimal APIs, Razor Pages, health checks, middleware, and IApplicationBuilder.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.