Skip to content

Grouping

Ieuan Walker edited this page Dec 30, 2025 · 4 revisions

Endpoint grouping allows you to organise related endpoints under common route prefixes and apply shared configuration. This is particularly useful for versioning, common middleware, or organising endpoints by feature area.

Creating an Endpoint Group

Create a class that implements IEndpointGroup:

using IeuanWalker.MinimalApi.Endpoints;

public class TodoEndpointGroup : IEndpointGroup
{
    public static RouteGroupBuilder Configure(WebApplication app)
    {
        return app
            .MapGroup("api/v{version:apiVersion}/todos")
            .WithTags("Todos")
            .RequireAuthorization(); // Apply authorization to all endpoints in this group
    }
}

Using Endpoint Groups

Reference the group in your endpoints using the Group<T>() extension method:

public class GetAllTodosEndpoint : IEndpointWithoutRequest<ResponseModel[]>
{
    public static void Configure(RouteHandlerBuilder builder)
    {
        builder
            .Group<TodoEndpointGroup>()  // Reference the endpoint group
            .Get()                       // Relative path within the group
            .WithSummary("Get all todos")
            .WithDescription("Retrieves all todos from the store")
            .Version(1.0);
    }

    public async Task<ResponseModel[]> Handle(CancellationToken ct)
    {
        // Implementation
        return await GetTodosAsync();
    }
}

This is the equivalent of the minimal api MapGroup extension method - https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/route-handlers?view=aspnetcore-9.0#route-groups

Clone this wiki locally