Hossein Dara
Hossein Dara

Reputation: 39

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CommandAPI.Dtos.CommandReadDto>' to 'Microsoft.AspNetCore.Mvc.ActionResult

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?

Upvotes: 0

Views: 43

Answers (1)

Betsq9
Betsq9

Reputation: 145

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

Upvotes: 1

Related Questions