Exemple #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()
            }));
        }
Exemple #2
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 RESTfulResult <object>
                {
                    StatusCode = StatusCodes.Status401Unauthorized,
                    Successed  = false,
                    Data       = null,
                    Errors     = "401 Unauthorized",
                    Extras     = UnifyResultContext.Take()
                }, JsonSerializerUtility.GetDefaultJsonSerializerOptions());

                break;

            // 处理 403 状态码
            case StatusCodes.Status403Forbidden:
                await context.Response.WriteAsJsonAsync(new RESTfulResult <object>
                {
                    StatusCode = StatusCodes.Status403Forbidden,
                    Successed  = false,
                    Data       = null,
                    Errors     = "403 Forbidden",
                    Extras     = UnifyResultContext.Take()
                }, JsonSerializerUtility.GetDefaultJsonSerializerOptions());

                break;

            default:
                break;
            }
        }
Exemple #3
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()
     }));
 }
Exemple #4
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,
                Extras = UnifyResultContext.Take()
            }));
        }
Exemple #5
0
        /// <summary>
        /// 异常返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnException(ExceptionContext context)
        {
            // 解析异常信息
            var(ErrorCode, ErrorContent) = UnifyResultContext.GetExceptionMetadata(context);

            return(new JsonResult(new RESTfulResult <object>
            {
                StatusCode = ErrorCode,
                Successed = false,
                Data = null,
                Errors = ErrorContent,
                Extras = UnifyResultContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }));
        }
Exemple #6
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 RESTfulResult <object>
                {
                    StatusCode = StatusCodes.Status401Unauthorized,
                    Successed  = false,
                    Data       = null,
                    Errors     = "401 Unauthorized",
                    Extras     = UnifyResultContext.Take()
                }, new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonSerializerUtility.EnabledPascalPropertyNaming ? null : JsonNamingPolicy.CamelCase
                });

                break;

            // 处理 403 状态码
            case StatusCodes.Status403Forbidden:
                await context.Response.WriteAsJsonAsync(new RESTfulResult <object>
                {
                    StatusCode = StatusCodes.Status403Forbidden,
                    Successed  = false,
                    Data       = null,
                    Errors     = "403 Forbidden",
                    Extras     = UnifyResultContext.Take()
                }, new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonSerializerUtility.EnabledPascalPropertyNaming ? null : JsonNamingPolicy.CamelCase
                });

                break;

            default:
                break;
            }
        }