Skip to content

Changing response type for automatic ModelState Validation #24950

Answered by HadiSDev
HadiSDev asked this question in General
Discussion options

You must be logged in to vote

Solved with this code:

            services.Configure<ApiBehaviorOptions>(apiBehaviorOptions => 
                    apiBehaviorOptions.InvalidModelStateResponseFactory = actionContext =>
                    {
                        var messages = actionContext.ModelState.Values.SelectMany(x => x.Errors.Select(m => m.ErrorMessage)).ToList();
                        return new BadRequestObjectResult(new ResponseBase<object>(){StatusCode = 400, Errors = messages, Data = null});
                    }
                );

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by HadiSDev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant