I'm making a Web API and I want to retrieve the object created by the CreateCommand
Method.
To do that, I'm using the CreateAtRoute
function to call the GetCommandById
function ,with the id of the created Command
as parameter, but I'm getting the following error:
" System.InvalidOperationException: No route matches the suppliedvalues."
This is my controller:
[Route("api/commands")][ApiController]public class CommandsController : Controller{ private readonly ICommanderRepo _repository; private readonly IMapper _mapper; public CommandsController(ICommanderRepo repository,IMapper mapper) { _repository = repository; _mapper = mapper; } [HttpGet] public ActionResult <IEnumerable<CommandReadDto>> GetAllCommands() { var commands = _repository.GetAllCommands(); return Ok(_mapper.Map<IEnumerable<CommandReadDto>>(commands)); } [HttpGet("{id}")] public ActionResult <CommandReadDto> GetCommandById(int id) { var command = _repository.GetCommandById(id); if(command != null) { return Ok(_mapper.Map<CommandReadDto>(command)); } return NotFound(); } [HttpPost] public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto) { var commandModel = _mapper.Map<Command>(commandCreateDto); _repository.CreateCommand(commandModel); _repository.SaveChanges(); var commandReadDto = _mapper.Map<CommandReadDto>(commandModel); var x = nameof(GetCommandById); return CreatedAtRoute(nameof(GetCommandById), new { id = commandReadDto.Id }, commandReadDto); }
I have already tried this (which didn't resolve the problem):
- Check if the parameters of both functions match
- Added to my Startup.cs :
services.AddControllers(options => options.SuppressAsyncSuffixInActionNames = false);
( I saw this on a post here in Stack Overflow)
What might be the problem?