-1

Is it possible to send HttpStatusCode.Conflict(409) from ValidationResult method of System.ComponentModel.DataAnnotations

By default it is returning HttpStatusCode.BadRequest(400). is there any possible way to send different status codes with ValidationResult method.

1 Answers1

0

Since you are using asp.net core you can override the ApiController attribute behavior in the StartUp > ConfigureServices

services.AddControllers()
                    .ConfigureApiBehaviorOptions(options => {
                        options.InvalidModelStateResponseFactory = context =>
                        {
                            var problemDetails = new ValidationProblemDetails(context.ModelState)
                            {
                                Status = StatusCodes.Status409Conflict,                            
                            };

                            return new UnprocessableEntityObjectResult(problemDetails){};
                        }; });
ChizT
  • 677
  • 3
  • 10
  • Thanks @ranton187, It solved the issue. In my case I just changed the UnprocessableEntityObjectResult(problemDetails) to ConflictObjectResult(problemDetails). And it is giving 409 conflict status code – Salman Tahir Sep 17 '21 at 08:25
  • @SalmanTahir my pleasure – ChizT Sep 17 '21 at 08:27