Skip to content

Endpoint name metadata should be set automatically to the method name when Map overload is called passing a MethodGroup #34540

Closed
@DamianEdwards

Description

@DamianEdwards

Endpoint names are used to lookup endpoints when generating links using LinkGenerator, and as their unique per application are a good candidate for using as the operationId for an endpoint in OpenAPI (Swagger) documents.

(domaindrivendev/Swashbuckle.AspNetCore#2165 is tracking setting the operationId from endpoint name by default in Swashbuckle.)

Endpoint names are set using the Microsoft.AspNetCore.Routing.IEndpointNameMetadata interface. In the framework today this can be set imperatively by adding an instance of Microsoft.AspNetCore.Routing.EndpointNameMetadata to the endpoint's metadata, e.g. builder.WithMetadata(new EndpointNameMetadata("GetTodoById")). Issues #34538 and #34539 are tracking providing further ways to define endpoint names manually.

In minimal APIs we should automatically set the endpoint name to the value of MethodInfo.Name when the endpoint is defined using a MethodGroup rather than a lambda or anonymous delegate, e.g.:

// These endpoints do not have names
app.MapGet("/hello", () => "Hello");
app.MapGet("/goodbye/{name}", (string name) => $"Goodbye {name}");

// These endpoints have their name set automatically
app.MapGet("/todos/{id}", GetTodoById);
app.MapPost("/todos", AddTodo);

async Task<IResult> GetTodoById(int id, TodoDb db)
{
    return await db.Todos.FindAsync(id)
        is Todo todo
            ? Results.Ok(todo)
            : Results.NotFound();
};

async Task<IResult> AddTodo(Todo todo, TodoDb db)
{
    db.Todos.Add(todo);
    await db.SaveChangesAsync();

    return Results.CreatedAtRoute(nameof(GetTodoById), todo);
}

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions