webapi 接口規則


  [HttpPost]
        [AuthorizeFilter]
        public HttpResponseMessage DeleteStudentInfo([FromBody] object value)
        {
            ApiResult<StudentEntity> res = new ApiResult<StudentEntity>();
            try
            {
                StudentEntity model = JsonConvert.DeserializeObject<StudentEntity>(JsonConvert.SerializeObject(value));
                if (model != null && model.id > 0)
                {
                    model.IsDelete = 1;
                    StudentLogic bll = new StudentLogic();
                    int result = 0;
                    result = bll.UpdateCommand(model);
                    if (result > 0)
                    {
                        res.ResultFlag = 1;
                        res.ResultMsg = "操作成功";
                        res.ResultObj = null;
                    }
                    else
                    {
                        res.ResultFlag = 0;
                        res.ResultMsg = "操作失敗";
                        res.ResultObj = null;
                    }
                }
                else
                {
                    res.ResultFlag = 0;
                    res.ResultMsg = "參數錯誤";
                    res.ResultObj = null;
                }
            }
            catch (Exception ex)
            {
                res.ResultFlag = 0;
                res.ResultMsg = ex.Message;
                res.ResultObj = null;
                //寫錯誤日志
                WebLogTool.WriteLog(ex, "StudentController-DeleteStudentInfo");
            }
            return HttpHelper.ResponseMessagetoJson(res);
        }

json 處理 

#region 程序集 Newtonsoft.Json.dll, v4.5.0.0
// D:\wanglei\Wisdom.JPClient\trunk\Code\Wisdom.JPClient.WebApi\bin\Newtonsoft.Json.dll
#endregion

 public class ApiResult<T>
    {
        int _ResultFlag;
        /// <summary>
        /// 接口調用狀態(1成功 0系統錯誤 其余狀態自定義)
        /// </summary>
        public int ResultFlag
        {
            get { return _ResultFlag; }
            set { _ResultFlag = value; }
        }
        string _ResultMsg;
        /// <summary>
        /// 接口返回消息
        /// </summary>
        public string ResultMsg
        {
            get { return _ResultMsg; }
            set { _ResultMsg = value; }
        }
        T _ResultObj;
        /// <summary>
        /// 接口返回對象
        /// </summary>
        public T ResultObj
        {
            get { return _ResultObj; }
            set { _ResultObj = value; }
        }
    }

    /// <summary>
    /// Web Api 返回對象
    /// </summary>
    public class ApiResult_<T>
    {
        string _ResultFlag;
        /// <summary>
        /// 接口調用狀態(1成功 0系統錯誤 其余狀態自定義)
        /// </summary>
        public string ResultFlag
        {
            get { return _ResultFlag; }
            set { _ResultFlag = value; }
        }
        string _ResultMsg;
        /// <summary>
        /// 接口返回消息
        /// </summary>
        public string ResultMsg
        {
            get { return _ResultMsg; }
            set { _ResultMsg = value; }
        }
        T _ResultObj;
        /// <summary>
        /// 接口返回對象
        /// </summary>
        public T ResultObj
        {
            get { return _ResultObj; }
            set { _ResultObj = value; }
        }
    }

api result 類

 public void DgUserDataBind()
        {
            if (pagerUser.PageIndex == 1)
            {
                pagerUser.PageSize = 20;//默認值20 自定義的時候需要傳
            }

            //查詢條件
            Hashtable ht = new Hashtable();
            //if (!string.IsNullOrEmpty(txtStuNo.Text.Trim()))//學員編號
            //{
            //    ht["StuNo"] = txtStuNo.Text.Trim();
            //}
      
            //if (cboClass.SelectedIndex > 0)//班級
            //{
            //    ht["Class"] = cboClass.SelectedValue.ToString();
            //}
         
            if (cboAppointmentStatus.SelectedIndex > 0)//預約狀態
            {
                ht["AppointmentStatus"] = cboAppointmentStatus.SelectedValue.ToString();
            }
            //if (cboTimeQuantum.SelectedIndex > 0)//時間段
            //{
            //    ht["TimeQuantum"] = ((CmbboxModel)cboTimeQuantum.SelectedItem).value;
            //}
            if (cboRegSite.SelectedIndex > 0)//報名點
            {
                ht["RegSite"] = cboRegSite.SelectedValue.ToString();
            }
            if (cboTrainingGround.SelectedIndex > 0)//訓練場
            {
                ht["TrainingGround"] = cboTrainingGround.SelectedValue.ToString();
            }
        
            if (!string.IsNullOrEmpty(cmbSchool.SelectedSchoolId))
            {
                ht["Schoolid"] = cmbSchool.SelectedSchoolId;
            }


            var lstMoreCondition = ((MoreSearchConditionBar.ItemsSource as ObservableCollection<MoreSearchConditionModel>) ?? new ObservableCollection<MoreSearchConditionModel>());
            var item = lstMoreCondition.Where(p => p.ConditionName == "教練員").FirstOrDefault();
            if (item != null)//教練
            {
                ht["Coach"] = item.ConditionText.Trim();
            }
             item = lstMoreCondition.Where(p => p.ConditionName == "預約開始日期").FirstOrDefault();
             if (item != null)//預約日期
            {
                ht["StartDate"] = item.ConditionText.Trim();
            }
             item = lstMoreCondition.Where(p => p.ConditionName == "預約結束日期").FirstOrDefault();
             if (item != null)
            {
                ht["EndDate"] = item.ConditionText.Trim();
            }
             item = lstMoreCondition.Where(p => p.ConditionName == "學車分類").FirstOrDefault();
             if (item != null)//學車分類
             {
                 ht["StuCarType"] = item.ConditionText.Trim();
             }
             item = lstMoreCondition.Where(p => p.ConditionName == "車型").FirstOrDefault();
             if (item != null)//車型
             {
                 ht["CarType"] = item.ConditionText.Trim();//((Wisdom.JPClient.Model.CmbboxModel)(cboCarType.SelectedItem)).value;
             }

            //初始化查詢參數
            PagePostParam postModel = new PagePostParam() { PageIndex = pagerUser.PageIndex, PageSize = pagerUser.PageSize, OrderBy = "id", OrderType = 0, ht = ht };
            //調用api取值
            KeyValuePair<bool, string> result = HttpHelper.PostWebRequest(BaseInfo.Cur.WebApiUrl + "StudentAppointment/getListByParam", postModel);
            PageResultTable pageresult = new PageResultTable();
            if (result.Key)
            {
                ApiResult<PageResultTable> res = JsonConvert.DeserializeObject<ApiResult<PageResultTable>>(result.Value);
                if (res.ResultFlag == 1)
                {
                    pageresult = res.ResultObj as PageResultTable;
                    grid1.ItemsSource = pageresult.dt.DefaultView;
                    DT = pageresult.dt;
                    pagerUser.TotalCount = pageresult.RowsCout;
                }
                else
                {
                    MsgBox.Show(res.ResultMsg);
                }
            }
            else
            {
                MsgBox.Show("操作失敗!");
            }
        }

上面是調用端解析寫法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM