Skip to content

Endpoint names are not inferred #27424

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
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions aspnetcore/fundamentals/minimal-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,17 @@ Route handlers are methods that execute when the route matches. Route handlers c

[!code-csharp[](minimal-apis/samples/WebMinAPIs/Program.cs?name=snippet_sm)]

### Name routes and link generation
### Named endpoints and link generation

Routes can be given names in order to generate URLs to the route. Using a named route avoids having to hard code paths in an app:
Endpoints can be given names in order to generate URLs to the endpoint. Using a named endpoint avoids having to hard code paths in an app:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Endpoints can be given names in order to generate URLs to the endpoint.

This sentence feels a little confusing. What exactly are we trying to say here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That endpoint names provide some indirection when generating links? I didn't write this originally, so I'm guessing. Do you have a suggestion for how we should reword this?

Copy link

@mauler2025 mauler2025 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing the readers with a lot of examples makes it much clearer. :-)

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

var users = new List<User>
 {
    new User(1,"A"),
    new User(2,"B"),
    new User(3,"C"),
 };

IResult GetUser(int id)
{
    return users.Find(u => u.id == id) is User user ?
        TypedResults.Ok(user) : TypedResults.NotFound();
}

IResult CreateUser(User user, LinkGenerator lg)
{
    users.Add(user);
    var path = lg.GetPathByName(nameof(GetUser), new { id = user.id })!;
    return TypedResults.Created(path, user);
}

var group = app.MapGroup("/users");

group
    .MapGet("/{id}", GetUser)
    .WithName(nameof(GetUser));

group.MapPost("/", CreateUser);

app.Run();

record class User(int id, string name);


[!code-csharp[](minimal-apis/samples/WebMinAPIs/Program.cs?name=snippet_nr)]

The preceding code displays `The link to the hello route is /hello` from the `/` endpoint.

Route names are inferred from method names if specified:

[!code-csharp[](minimal-apis/samples/WebMinAPIs/Program.cs?name=snippet_nr2)]
The preceding code displays `The link to the hello endpoint is /hello` from the `/` endpoint.

**REVIEW**: `{linker.GetPathByName("Hi", values: null)}` is null in the preceding code.
**NOTE**: Endpoint names are case sensitive.

**NOTE**: Route names are case sensitive.

Route names:
Endpoint names:

* Must be globally unique.
* Are used as the OpenAPI operation id when OpenAPI support is enabled. For more information, see [OpenAPI](xref:fundamentals/minimal-apis/openapi).
Expand Down