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:
[HttpPost] | |
[Consumes(MediaTypeNames.Application.Json)] | |
[ProducesResponseType(StatusCodes.Status201Created)] | |
[ProducesResponseType(StatusCodes.Status400BadRequest)] | |
public async Task<IActionResult> CreateAsync(Product product) | |
{ | |
if (product.Description.Contains("XYZ Widget")) | |
{ | |
return BadRequest(); | |
} | |
await _repository.AddProductAsync(product); | |
return CreatedAtAction(nameof(GetById), new { id = product.Id }); | |
} |
And here is the related GetById method:
[HttpGet("{id}")] | |
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Product))] | |
[ProducesResponseType(StatusCodes.Status404NotFound)] | |
public IActionResult GetById(int id) | |
{ | |
if (!_repository.TryGetProduct(id, out var product)) | |
{ | |
return NotFound(); | |
} | |
return Ok(product); | |
} |
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:
return CreatedAtAction(nameof(GetById), new { id = product.Id }, product); |