-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Closed
Labels
✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Resolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I had to replace
CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id });
by explicit
var url = Url.Action(nameof(GetTodoItem), new { id = todoItem.Id });
return Created(url, todoItem);
In order to not get "System.InvalidOperationException: No route matches the supplied values." when posting
{ "Title":"Task One", "IsComplete": false }
I assume the problem is that the Guid somehow.
The code is
public class TodoItem
{
[Required]
public Guid Id { get; init; }
[Required]
[StringLength(255)]
public string Title { get; init; }
[Required]
public bool IsComplete { get; init; }
}
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
{
context.TodoItems.Add(todoItem);
try
{
await context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (TodoItemExists(todoItem.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id });
}
Expected Behavior
I expected CreatedAtAction to work as the more explicit code.
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
8.0.204
Anything else?
aspnet core 8.0.4
mrememisaac
Metadata
Metadata
Assignees
Labels
✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Resolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templates