예제 #1
0
        /// <summary>
        /// 成功返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnSuccessed(ActionExecutedContext context)
        {
            object data;

            // 处理内容结果
            if (context.Result is ContentResult contentResult)
            {
                data = contentResult.Content;
            }
            // 处理对象结果
            else if (context.Result is ObjectResult objectResult)
            {
                data = objectResult.Value;
            }
            else
            {
                return(null);
            }

            return(new JsonResult(new RESTfulResult <object>
            {
                StatusCode = context.Result is EmptyResult ? StatusCodes.Status204NoContent : StatusCodes.Status200OK,  // 处理没有返回值情况 204
                Successed = true,
                Data = data,
                Errors = null,
                Extras = UnifyResultContext.Take()
            }));
        }
예제 #2
0
        /// <summary>
        /// 处理规范化结果
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            // 排除 Mvc 视图
            var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;

            if (actionDescriptor.ControllerTypeInfo.BaseType == typeof(Controller))
            {
                await next();

                return;
            }

            var actionExecutedContext = await next();

            // 如果没有异常再执行
            if (actionExecutedContext.Exception == null && !UnifyResultContext.IsSkipOnSuccessUnifyHandler(actionDescriptor.MethodInfo, out var unifyResult))
            {
                // 处理规范化结果
                if (unifyResult != null && context.Result == null)
                {
                    var result = unifyResult.OnSuccessed(actionExecutedContext);
                    if (result != null)
                    {
                        actionExecutedContext.Result = result;
                    }
                }
            }
        }
        /// <summary>
        /// 中间件执行方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
        {
            await _next(context);

            // 处理规范化结果
            if (!UnifyResultContext.IsSkipUnifyHandler(context, out var unifyResult))
            {
                await unifyResult.OnResponseStatusCodes(context, context.Response.StatusCode);
            }
        }
예제 #4
0
 /// <summary>
 /// 验证失败返回值
 /// </summary>
 /// <param name="context"></param>
 /// <param name="modelStates"></param>
 /// <param name="validationResults"></param>
 /// <param name="validateFaildMessage"></param>
 /// <returns></returns>
 public IActionResult OnValidateFailed(ActionExecutingContext context, ModelStateDictionary modelStates, Dictionary <string, IEnumerable <string> > validationResults, string validateFaildMessage)
 {
     return(new JsonResult(new RESTfulResult <object>
     {
         StatusCode = StatusCodes.Status400BadRequest,
         Successed = false,
         Data = null,
         Errors = validationResults,
         Extras = UnifyResultContext.Take()
     }));
 }
예제 #5
0
        /// <summary>
        /// 异常返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnException(ExceptionContext context)
        {
            // 解析异常信息
            var(ErrorCode, ErrorObject) = UnifyResultContext.GetExceptionMetadata(context);

            return(new JsonResult(new RESTfulResult <object>
            {
                StatusCode = ErrorCode,
                Successed = false,
                Data = null,
                Errors = ErrorObject
            }));
        }