首页 > 技术文章 > .net mvc webapi使用异步请求处理并发

jiamengyang 2018-09-06 17:58 原文

model.cs中

 #region 根据学年获取老师在的班级
    public class TeacherClassForYearPara
    {
        public string YearCode { set; get; }
        public string UserCode { set; get; }

    }
    public class TeacherClassForYearRet: BasPostMsg
    {
        public string ClassCode { set; get; }
        public string ClassName { set; get; }

    }
    #endregion

repository.cs中

        public async Task<TeacherClassForYearRet> TeacherClassForYear(TeacherClassForYearPara para)
        {
            TeacherClassForYearRet ret = new TeacherClassForYearRet();
            try
            {

                await Task.Run(() =>
                {
                    if (string.IsNullOrEmpty(para.UserCode))
                        throw new Exception("UserCode不能为空");
                    if (string.IsNullOrEmpty(para.YearCode))
                        throw new Exception("YearCode不能为空");

                    DbCommand cmd = db.GetStoredProcCommond("TeacherClassForYear_P");
                    db.AddInParameter(cmd, "@UserCode", DbType.Int32, para.UserCode);
                    db.AddInParameter(cmd, "@YearCode", DbType.String, para.YearCode);
                    DataTable dt = db.ExecuteDataTable(cmd);
                    if (dt.Rows.Count > 0)
                    {
                        ret.ClassCode = dt.Rows[0]["ClassCode"].ToString();
                        ret.ClassName = dt.Rows[0]["ClassName"].ToString();

                    }
                    ret.Status = "1";
                    ret.Msg = "请求成功!";
                }
            );

            }
            catch (Exception e)
            {
                ret.Status = "0";
                ret.Msg = e.Message;
            }
            return ret;
        }

  controller.cs中

 #region 根据学年获取老师在的班级
        [HttpPost]
        public async Task<IHttpActionResult> TeacherClassForYear([FromBody]TeacherClassForYearPara para)
                    => Ok(await repo.TeacherClassForYear(para));
        #endregion

  

推荐阅读