Esempio n. 1
0
        /// <summary>
        /// 成功返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnSucceeded(ActionExecutedContext context)
        {
            object data;

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

            return(new JsonResult(new ApiResponse <object>
            {
                Code = context.Result is EmptyResult ? StatusCodes.Status204NoContent : StatusCodes.Status200OK,  // 处理没有返回值情况 204
                Success = true,
                Data = data,
                Msg = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }));
        }
Esempio n. 2
0
 /// <summary>
 /// 验证失败返回值
 /// </summary>
 /// <param name="context"></param>
 /// <param name="modelStates"></param>
 /// <param name="validationResults"></param>
 /// <param name="validateFailedMessage"></param>
 /// <returns></returns>
 public IActionResult OnValidateFailed(ActionExecutingContext context, ModelStateDictionary modelStates, Dictionary <string, IEnumerable <string> > validationResults, string validateFailedMessage)
 {
     return(new JsonResult(new ApiResponse <object>
     {
         Code = StatusCodes.Status400BadRequest,
         Success = false,
         Data = null,
         Msg = validationResults,
         Extras = UnifyContext.Take(),
         Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
     }));
 }
Esempio n. 3
0
 /// <summary>
 /// 异常返回值
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public IActionResult OnException(ExceptionContext context)
 {
     // 解析异常信息
     var(StatusCode, Errors) = UnifyContext.GetExceptionMetadata(context);
     return(new JsonResult(new ApiResponse <object>
     {
         Code = StatusCode,
         Success = false,
         Data = null,
         Msg = Errors,
         Extras = UnifyContext.Take(),
         Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
     }));
 }
        /// <summary>
        /// 异常返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnException(ExceptionContext context)
        {
            // 解析异常信息
            var(StatusCode, ErrorCode, Errors) = UnifyContext.GetExceptionMetadata(context);

            return(new JsonResult(new RESTfulResult <object>
            {
                Code = StatusCode,
                Succeeded = false,
                Data = null,
                Msg = context.Exception is AppFriendlyException ? Errors : "系统异常,请联系管理员",//Errors
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }));
        }
Esempio n. 5
0
        /// <summary>
        /// 处理输出状态码
        /// </summary>
        /// <param name="context"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        public async Task OnResponseStatusCodes(HttpContext context, int statusCode)
        {
            switch (statusCode)
            {
            // 处理 401 状态码
            case StatusCodes.Status401Unauthorized:
                await context.Response.WriteAsJsonAsync(new ApiResponse <object>
                {
                    Code      = StatusCodes.Status401Unauthorized,
                    Success   = false,
                    Data      = null,
                    Msg       = "401 Unauthorized",
                    Extras    = UnifyContext.Take(),
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, JsonSerializerUtility.GetDefaultJsonSerializerOptions());

                break;

            // 处理 403 状态码
            case StatusCodes.Status403Forbidden:
                await context.Response.WriteAsJsonAsync(new ApiResponse <object>
                {
                    Code      = StatusCodes.Status403Forbidden,
                    Success   = false,
                    Data      = null,
                    Msg       = "403 Forbidden",
                    Extras    = UnifyContext.Take(),
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, JsonSerializerUtility.GetDefaultJsonSerializerOptions());

                break;

            default:
                break;
            }
        }