如何在 web api 2 中为 get 方法编写异步 web api?
how to write async web api for get method in web api 2?
显示 "this asynch method lacks await" 的警告。请建议在异步方法上添加 await 的位置?
public async Task<IHttpActionResult> GetEmployeeDetails(string id,string StartDate, string EndDate)
{
DateTime sd = Convert.ToDateTime(StartDate);
DateTime ed = Convert.ToDateTime(EndDate);
using (EmployeeEntities db = new EmployeeEntities ())
{
try
{
return Ok(db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
}
catch (Exception)
{
return BadRequest("Sorry Error Found!!!");
}
}
}
您可以在使用 ToListAsync()
的 return 语句中使用它
return Ok(await db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
显示 "this asynch method lacks await" 的警告。请建议在异步方法上添加 await 的位置?
public async Task<IHttpActionResult> GetEmployeeDetails(string id,string StartDate, string EndDate)
{
DateTime sd = Convert.ToDateTime(StartDate);
DateTime ed = Convert.ToDateTime(EndDate);
using (EmployeeEntities db = new EmployeeEntities ())
{
try
{
return Ok(db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
}
catch (Exception)
{
return BadRequest("Sorry Error Found!!!");
}
}
}
您可以在使用 ToListAsync()
return Ok(await db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());