首页 > 解决方案 > 如何在 web api 2 中为 get 方法编写异步 web api?

问题描述

显示“此异步方法缺少等待”的警告。请建议在我的异步方法中添加等待的位置?

 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!!!");
                }
            }
        }

标签: asp.net-web-api2

解决方案


您可以在您正在使用的 return 语句中使用它ToListAsync()

return Ok(await db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());

推荐阅读