【OF框架】定义框架标准WebApi,按照规范返回状态信息及数据信息

准备


 

 了解框架基本应用,已经完成Controller创建。

 

一、定义框架标准WebApi


 

 一个标准的WebApi,包含预定义的入参和回参类型

 

 

入参为CallParams,需要增加FromBody声明,代码如下:

        public override ActionResult<CallResult> Query()
        {
            var list = _IEmployeeService.Query(Request.Params("DepartmentID"), this.ThisPageInfo);
            return CallResult.Create(list);
        }

        public ActionResult<CallResult> DisableOrEnabled([FromBody]CallParams param)
        {
            string id = param["Id"];
            string strStatus = param["status"];

            int status = (!string.IsNullOrEmpty(strStatus) && Convert.ToBoolean(strStatus)) ? 1 : 0;

            if (_IEmployeeService.DisableOrEnabled(id, status) <= 0)
                return CallResult.Create(CallResultCode.NoChanged, "操作失败");

            return CallResult.Success;
        }

 

 

入参也可以是实体类型,不需要添加FromBody声明,代码如下:

        public override ActionResult<CallResult> Save(sys_employee entity)
        {
            if (_IEmployeeService.Save(entity) <= 0)
                return CallResult.Create(CallResultCode.NoChanged, "保存失败");

            return CallResult.Success;
        }

 

 

 

二、入参CallParams对象介绍


 

CallParams是一个继承Dictionary<string, object>的对象,方便取出参数,重载了索引取值方法,直接返回字符串。

    public class CallParams : Dictionary<string, object>
    {
        public new string this[string key]
        {
            get
            {
                if (base.TryGetValue(key, out object item))
                    return Convert.ToString(item);
                return string.Empty;
            }
        }
    }

 

 

 

三、回参CallResult多种返回写法


 

            //返回成功
            return CallResult.Success;
            //返回状态
            return CallResult.Create(CallResultCode.NoChanged);
            //返回状态,备注
            return CallResult.Create(CallResultCode.NoChanged, "操作失败");
            //返回数据
            return CallResult.Create(list);
            //返回状态,备注,数据
            return CallResult.Create(CallResultCode.Success, "操作成功", list);

 

posted on 2019-05-21 17:28  陈银鑫  阅读(452)  评论(0编辑  收藏  举报