Exemplo n.º 1
0
        /// <summary>
        /// 处理输出状态码
        /// </summary>
        /// <param name="context"></param>
        /// <param name="statusCode"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultStatusCodesOptions options)
        {
            // 设置响应状态码
            UnifyContext.SetResponseStatusCodes(context, statusCode, options);

            switch (statusCode)
            {
            // 处理 401 状态码
            case StatusCodes.Status401Unauthorized:
                await context.Response.WriteAsJsonAsync(new RESTfulResult <object>
                {
                    StatusCode = StatusCodes.Status401Unauthorized,
                    Succeeded  = false,
                    Data       = null,
                    Errors     = "401 Unauthorized",
                    Extras     = UnifyContext.Take(),
                    Timestamp  = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                });

                break;

            // 处理 403 状态码
            case StatusCodes.Status403Forbidden:
                await context.Response.WriteAsJsonAsync(new RESTfulResult <object>
                {
                    StatusCode = StatusCodes.Status403Forbidden,
                    Succeeded  = false,
                    Data       = null,
                    Errors     = "403 Forbidden",
                    Extras     = UnifyContext.Take(),
                    Timestamp  = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                });

                break;

            default:
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 特定状态码返回值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="statusCode"></param>
        /// <param name="unifyResultSettings"></param>
        /// <returns></returns>
        public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings)
        {
            // 设置响应状态码
            UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);

            switch (statusCode)
            {
            // 处理 401 状态码
            case StatusCodes.Status401Unauthorized:
                await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors : "401 Unauthorized")
                                                        , App.GetOptions <JsonOptions>()?.JsonSerializerOptions);

                break;

            // 处理 403 状态码
            case StatusCodes.Status403Forbidden:
                await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors : "403 Forbidden")
                                                        , App.GetOptions <JsonOptions>()?.JsonSerializerOptions);

                break;

            default: break;
            }
        }