dotnet / dotnet/aspnetcore

Add a `CreatedAtRoute` result for multiple routes

Open
#40,856 3 comments 1 reaction 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

Normally, when doing a POST in an API, I can return a `CreatedAtRoute` like so:

```c#
[HttpPost(Name = "AddItem")]
public async Task> AddItem([FromBody]ItemForCreationDto itemForCreation)
{
var command = new AddItem.AddItemCommand(itemForCreation);
var commandResponse = await _mediator.Send(command);

return CreatedAtRoute("GetItem",
new { commandResponse.Id },
commandResponse);
}
```

But if I tweak this do become a batch add that created multiple items, I don't have a good path forward like I would with adding one item.

### Describe the solution you'd like

Before I noticed how this was worked, I was tempted to do something like this:

```c#
[HttpPost("batch", Name = "AddItemList")]
public async Task> AddItem([FromBody]IEnumerable itemForCreation,
[FromQuery(Name = "parentId"), BindRequired] Guid parentId)
{
var command = new AddItemList.AddItemListCommand(itemForCreation, parentId);
var commandResponse = await _mediator.Send(command);

return CreatedAtRoute("GetItem",
new { Id = commandResponse.Select(c => c.Id) },
commandResponse);
}
```

I'd probably want to at least update it to a new method called `CreatedAtRoutes`, but maybe there's a better API for the particular use case.

Regardless, seems like a gap for what I would guess is a fairly common scenario.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.