[ASP.NET Controller] No route matches the supplied values

I'm trying to redirect my call to get the animal information from the create action method using this
return CreatedAtAction(nameof(GetById), new { Id = animal.Id }, _mapper.Map<AnimalDTO>(animal));
return CreatedAtAction(nameof(GetById), new { Id = animal.Id }, _mapper.Map<AnimalDTO>(animal));
And this is my GetById Action method
[HttpGet("{animalId}")]
public async Task<IActionResult> GetById(int animalId, CancellationToken cancellationToken = default)
{
var animal = await _animalRepository.FindByIdAsync(animalId, cancellationToken);
return animal != null ? Ok(_mapper.Map<AnimalDTO>(animal)) : NotFound("Unable to find the requested animal");
}
[HttpGet("{animalId}")]
public async Task<IActionResult> GetById(int animalId, CancellationToken cancellationToken = default)
{
var animal = await _animalRepository.FindByIdAsync(animalId, cancellationToken);
return animal != null ? Ok(_mapper.Map<AnimalDTO>(animal)) : NotFound("Unable to find the requested animal");
}
3 Replies
Angius
Angius2y ago
The route param is animalId not Id
TotechsStrypper
thanks It doesnt trigger the breakpoint that set in GetById method ?
Angius
Angius2y ago
IIRC it doesn't actually call that action, because why would it? It just returns the 201 Created response and sets the Location header to the route from which you can fetch that resource That's why it needs the action name and the parameters, to construct that route