default it will without control, but we can make it in control by using below solution: InvalidModelStateResponseExtension
InvalidModelStateResponseExtension
/// <summary>
/// Return common http response for model validation error
/// https://docs.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-2.2#validation-failure-error-response
/// </summary>
public static class InvalidModelStateResponseExtension
{
public static void ConfigureInvalidModelStateResponseApiBehavior(this IServiceCollection services)
{
services.Configure<ApiBehaviorOptions>(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var problemDetails = new ValidationProblemDetails(context.ModelState);
var errorResponse = new ErrorResponse()
{
Code = ErrorMessages.INVALID_PARAMETER_ERROR_CODE.ToString(),
Message = problemDetails.Title,
Parameters = problemDetails.Errors
};
return new BadRequestObjectResult(errorResponse);
};
});
}
}
controller
[Route("api/[controller]/[action]")]
[ApiController]
public class xxxController : ControllerBase
{
// expose uri: xxx/UpdateStatus
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK)]
public ActionResult UpdateStatus([FromBody] ReqestModel requestBody)
{
return Ok();
}
[HttpGet("{path-args}")]
public IList<resposneModel> Getxx(string path-args)
{
// .netcore will do http resposne, default is application/json,
// fotunately, we can specify data format in startup
return new List<resposneModel>();
}
}
requirement
Bad Request
InvalidModelStateResponseExtension
InvalidModelStateResponseExtension
controller
start up