Home > Software engineering >  Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CommandAPI.Dtos.Comma
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CommandAPI.Dtos.Comma

Time:06-26

Every time I test for the target action: GetAllCommands(), I face this error!

Here my code:

[HttpGet]
public ActionResult<IEnumerable<CommandReadDto>> GetAllCommands()
{
      var commandItems = _repository.GetAllCommands();
      return _mapper.Map<IEnumerable<CommandReadDto>>(commandItems);//error-pane code
            
}

But when I return the result set as follow, it works correctly:

return Ok(_mapper.Map<IEnumerable<CommandReadDto>>(commandItems));

could anyone explain it to me?

CodePudding user response:

This is because the ActionResult types represent various HTTP status codes.

You have an ActionResult<IEnumerable<CommandReadDto>> endpoint return type, but you are trying to return an IEnumerable<CommandReadDto> with no defined code status, so it needs to be wrapped with Ok() or NotFound() methods, etc.

If you want to return IEnumerable<CommandReadDto>, then remove ActionResult from the return type and leave only IEnumerable<CommandReadDto> there, then it will work and automatically return status code 200

  • Related