Mapping a group of endpoints to a resource url #1476
-
|
Consider two group of API endpoints, TodoLists and TodoItems. How could I organise the endpoint mapping so that todo-items are grouped as resources under their respective todo-lists? I would prefer to leverage the existing |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I decided to opt-out of the existing This introduces a new dependency but I believe this comes with a more simple implementation, adheres to the default behaviour of the existing The magic and heavylifting is implemented in the The The Then in I have added some additional extension methods to infer the group and method names, to avoid having to explicitly decorate my method delegates with attributes but this is syntactic sugar: A sample implementation of the group of endpoints, implementing And that's it! 😃 When implementing this pattern, the classes |
Beta Was this translation helpful? Give feedback.
-
|
@mrhighstone, thanks for raising this. On nested resource paths The template convention maps each public class TodoItems : IEndpointGroup
{
public static string RoutePrefix => "/api/TodoLists/{todoListId}/TodoItems";
public static void Map(RouteGroupBuilder groupBuilder)
{
groupBuilder.MapPatch(UpdateTodoItemDetail, "UpdateDetail/{id}").RequireAuthorization();
// ...
}
public static async Task<Results<NoContent, BadRequest>> UpdateTodoItemDetail(
ISender sender, int todoListId, int id, UpdateTodoItemDetailCommand command)
{
// ...
}
}The On your alternative approach Your approach is solid, and the |
Beta Was this translation helpful? Give feedback.
@mrhighstone, thanks for raising this.
On nested resource paths
The template convention maps each
IEndpointGroupto a flat/api/{ClassName}prefix by default. As of Release v10.6.0, groups can override that convention by declaring aRoutePrefixproperty: