Skip to content

Can't find minimal endpoint named via WithName() when using Results.CreatedAt #35314

@DamianEdwards

Description

@DamianEdwards

Calling .WithName("ApiName") on a minimal endpoint API does not add a RouteNameMetadata with specified name to the endpoint's metadata, just an EndpointNameMetadata. This means URLs can't be generated for the endpoint by IUrlHelper using route values, which includes the Results.CreatedAtRoute method.

We should update .WithName(string name) to also add the RouteNameMetadata.

Repro code:

app.MapGet("/todos/{id}", async (int id, TodoDb db) =>
    {
        return await db.Todos.FindAsync(id)
            is Todo todo
                ? Results.Ok(todo)
                : Results.NotFound();
    })
    .WithName("GetTodoById")
    .Produces<Todo>()
    .Produces(StatusCodes.Status404NotFound);

app.MapPost("/todos", async (Todo todo, TodoDb db) =>
    {
        db.Todos.Add(todo);
        await db.SaveChangesAsync();

        return Results.CreatedAtRoute("GetTodoById", new { todo.Id } , todo);
    })
    .WithName("AddTodo")
    .ProducesValidationProblem()
    .Produces<Todo>(StatusCodes.Status201Created);

Metadata

Metadata

Assignees

Labels

area-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