After creating a new item I wanted to return the location where the newly created item could be fetched. So I used the CreatedAtActionResult to specify the action.
Here is the code I was using:
And here is the related GetById method:
I wouldn’t be writing this blog post if everything worked as expected. When calling the CreateAsync method, I got the following error message:
No route matches the supplied values.
I couldn’t figure out what I was doing wrong until I took a look at the different overloads of CreatedAtAction:
If you look at the code above, you notice that I’m using the second overload. This overload expects 2 parameters:
- actionname: The name of the action to use for generating the URL
- value: The content value to format in the entity body
I was passing a‘new { id = product.Id } ’value expecting that it would be matched to the ‘id’ parameter that the route was expecting. This turned out not to be the case as the second overload uses the passed object as the body of the response.
To get the behavior I want I need to use the first overload and pass both a routevalue object and a value object: