Skip to content

Allow setting EndpointNameMetadata for minimal APIs via a first-class extension method #34538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #34514
DamianEdwards opened this issue Jul 20, 2021 · 1 comment
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:1 Work that is critical for the release, but we could probably ship without
Milestone

Comments

@DamianEdwards
Copy link
Member

DamianEdwards commented Jul 20, 2021

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")).

We should add a new extenstion method to allow setting the endpoint name in a more first-class fashion:

using System;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;

namespace Microsoft.AspNetCore.Builder;

public static class RoutingEndpointConventionBuilderExtensions
{
+     /// <summary>
+     /// Adds an <see cref="EndpointNameMetadata"/> item to the <see cref="IEndpointConventionBuilder"/> for all endpoints produced by the builder.
+     /// </summary>
+     /// <remarks>
+     /// Endpoint names must be unique within an application, and can be used to unambiguously
+     /// identify a desired endpoint for URI generation using Microsoft.AspNetCore.Routing.LinkGenerator.
+     /// </remarks>
+     /// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
+     /// <param name="name">The endpoint name.</param>
+     /// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
+     public static TBuilder WithName<TBuilder>(this TBuilder  builder, string name) where TBuilder : IEndpointConventionBuilder
+     {
+         builder.WithMetadata(new EndpointNameMetadata(name));
+ 
+         return builder;
+     }
}

The extension method could be used in the following way:

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");

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");
@DamianEdwards DamianEdwards added area-runtime feature-minimal-actions Controller-like actions for endpoint routing labels Jul 20, 2021
@DamianEdwards DamianEdwards changed the title Setting EndpointNameMetadata for minimal APIs and having it be used as operationId for OpenAPI documents by default Allow setting EndpointNameMetadata for minimal APIs via a first-class extension method Jul 20, 2021
@ghost
Copy link

ghost commented Jul 20, 2021

Thanks for contacting us.

We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@rafikiassumani-msft rafikiassumani-msft added the enhancement This issue represents an ask for new feature or an enhancement to an existing one label Jul 20, 2021
@rafikiassumani-msft rafikiassumani-msft added the Priority:1 Work that is critical for the release, but we could probably ship without label Jul 22, 2021
@davidfowl davidfowl added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-runtime labels Jul 31, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2021
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:1 Work that is critical for the release, but we could probably ship without
Projects
None yet
Development

No branches or pull requests

6 participants