Skip to content

When using CreatedAtAction and Guid as Id, I get "System.InvalidOperationException: No route matches the supplied values."  #55338

@mattiasw2

Description

@mattiasw2

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions