I have this code but the controller is expecting a return type of ActionResult. Why is the controller is accepting a Task> like this return await _context.Users.ToListAsync();
(the default code when a controller is generated)?
//controller
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
return await _userService.GetAll();
}
//service method
public async Task<IEnumerable<User>> GetAll()
{
return await _db.Users.ToListAsync();
}
I believe you need something like:
public async Task<ActionResult<IEnumerable<User>>> MyController()
{
var res = await _userService.GetAll();
if (res == null)
{
return NotFound();
}
return Ok(res);
}
As a side note, I recommend changing IEnumerable<>
to IList<>
. It's not strictly correct to return a enumerable here and you can also run into "multiple enumeration" issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.