ABP框架笔记1-返回结构

  ABP框架当中所有结果都会被封装,框架中核心类 AbpJsonResult ,它继承于JsonResult 。

  其最重要的两个属性为

  1.CamelCase

    小驼峰格式,默认为true

  2.Indented

    是否缩进,默认为false

  框架中AbpController重载了Controller的Json()方法,强制所有返回的Json格式数据为AbpJsonResult类型

  可以看到 只要我们的接口方法中含有DontWrapResult就会被忽略,不再使用框架封装。所以我们可以在我们的Service层的相应的方法上加上[DontWrapResult]即可完成自定义封装。

  

        /// <summary>
        /// Json the specified data, contentType, contentEncoding and behavior.
        /// </summary>
        /// <param name="data">Data.</param>
        /// <param name="contentType">Content type.</param>
        /// <param name="contentEncoding">Content encoding.</param>
        /// <param name="behavior">Behavior.</param>
        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            if (_wrapResultAttribute != null && !_wrapResultAttribute.WrapOnSuccess)
            {
                return base.Json(data, contentType, contentEncoding, behavior);
            }

            return AbpJson(data, contentType, contentEncoding, behavior);
        }

        protected virtual AbpJsonResult AbpJson(
            object data,
            string contentType = null,
            Encoding contentEncoding = null,
            JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet,
            bool wrapResult = true,
            bool camelCase = true,
            bool indented = false)
        {
            if (wrapResult)
            {
                if (data == null)
                {
                    data = new AjaxResponse();
                }
                else if (!(data is AjaxResponseBase))
                {
                    data = new AjaxResponse(data);
                }
            }

            return new AbpJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                CamelCase = camelCase,
                Indented = indented
            };
        }

 

 

  

posted @ 2022-06-30 15:40  kenneth_kung  阅读(334)  评论(0编辑  收藏  举报