public static IMvcBuilder ConfigureMvcProblemDetailsInvalidModelStateFactory(this IMvcBuilder builder, Action <ProblemDetailsInvalidModelStateFactoryOptions> setupAction = null)
        {
            var services = builder.Services;

            var problemDetailsInvalidModelStateFactoryOptions = new ProblemDetailsInvalidModelStateFactoryOptions();

            if (setupAction != null)
            {
                setupAction(problemDetailsInvalidModelStateFactoryOptions);
            }

            services.Configure <ApiBehaviorOptions>(options =>
            {
                //Overrides the default InvalidModelStateResponseFactory, adds traceId and timeGenerated to the ProblemDetails response.
                options.ConfigureProblemDetailsInvalidModelStateFactory(problemDetailsInvalidModelStateFactoryOptions);
            });


            if (problemDetailsInvalidModelStateFactoryOptions.ConfigureApiBehaviorOptions != null)
            {
                services.Configure(problemDetailsInvalidModelStateFactoryOptions.ConfigureApiBehaviorOptions);
            }

            return(builder);
        }
Example #2
0
        //Needs to be after AddMvc or use ConfigureApiBehaviourOptions
        public static void ConfigureProblemDetailsInvalidModelStateFactory(this ApiBehaviorOptions options, ProblemDetailsInvalidModelStateFactoryOptions problemDetailsInvalidModelStateFactoryOptions)
        {
            //400
            //401
            //403
            //404
            //406
            //409
            //415
            //422
            options.ClientErrorMapping[StatusCodes.Status500InternalServerError] = new ClientErrorData
            {
                Link  = "about:blank",
                Title = "An error has occured.",
            };

            options.ClientErrorMapping[499] = new ClientErrorData
            {
                Link  = "about:blank",
                Title = "The request was cancelled.",
            };

            options.ClientErrorMapping[StatusCodes.Status504GatewayTimeout] = new ClientErrorData
            {
                Link  = "about:blank",
                Title = "The request timed out.",
            };

            options.ClientErrorMapping[StatusCodes.Status422UnprocessableEntity] = new ClientErrorData
            {
                Link  = "https://tools.ietf.org/html/rfc4918#section-11.2",
                Title = "One or more validation errors occurred.", //Unprocessable Entity
            };

            options.InvalidModelStateResponseFactory = problemDetailsInvalidModelStateFactoryOptions.InvalidModelStateResponseAbstractFactory(problemDetailsInvalidModelStateFactoryOptions.EnableAngularErrors);
        }