webapi 接口规则

Posted 雪原日暮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("操作失败!");
            }
        }

上面是调用端解析写法

以上是关于webapi 接口规则的主要内容,如果未能解决你的问题,请参考以下文章

Web API接口

WebApi接口 - 如何在应用中调用webapi接口

java 注册过程 - android片段,Asp.net WebApi

Web API 接口规范

怎样操作WebAPI接口

怎样操作WebAPI接口