I have this code which uses data annotation as validation.
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly IAccountService _accountService;
/// <param name="accountService"></param>
public AccountController(IAccountService accountService)
{
_accountService = accountService;
}
[HttpGet]
[Route("IsAccountClosed/{nric}")]
public async Task<ActionResult> IsAccountClosed([Required] string nric)
{
// code removed for brevity
}
}
When I test it by passing it empty string (with a space), it returns a 400 status code. Below is the output in swagger.
My question is why by default it returns 400 status code instead of 422 status code which is more accurate status code? How do I change it to 422 status code with the content-type still application/problem json
?
After applying the suggestion from Yong Shun, I can 422 status code and content-type is application/problem json
but the response body is different as shown below.
CodePudding user response:
Update
After doing some research and trial and error,