コード例 #1
0
        public static IApplicationBuilder UseStatusCodePagesConfiguration(this IApplicationBuilder app)
        {
            app.UseStatusCodePages(async context =>
            {
                var req = context.HttpContext.Request;
                var res = context.HttpContext.Response;

                res.ContentType = "application/json";

                switch (res.StatusCode)
                {
                case (int)HttpStatusCode.Unauthorized:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultUnauthorizedResponse()));
                    break;

                case (int)HttpStatusCode.Forbidden:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultForbiddenResponse()));
                    break;

                case (int)HttpStatusCode.NotFound:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultNotFoundResponse()));
                    break;

                case (int)HttpStatusCode.BadRequest:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultBadRequestResponse()));
                    break;

                case (int)HttpStatusCode.MethodNotAllowed:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultMethodNotAllowedResponse()));
                    break;

                case (int)HttpStatusCode.RequestTimeout:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultRequestTimeoutResponse()));
                    break;

                case (int)HttpStatusCode.InternalServerError:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultInternalServerErrorResponse()));
                    break;

                default:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultInternalServerErrorResponse()));
                    break;
                }
            });

            return(app);
        }