I am using ASP.NET Core 5.0 and I have Users Controller with Register method, which receives UserRegisterInputModel. The problem is that all responses from my API are in specific format, but the ApiController auto-validates the input model and returns BadResponse in another format.
This is my abstract response model
public abstract class ResponseModel
{
public ResponseModel(bool successfull, int statusCode)
{
this.Successfull = successfull;
this.StatusCode = statusCode;
this.ErrorMessages = new List<string>();
}
public bool Successfull { get; set; }
public int StatusCode { get; set; }
public List<string> ErrorMessages { get; set; }
public object Data { get; set; }
}
And this is my BadResponseModel
public class BadResponseModel : ResponseModel
{
public BadResponseModel()
: base(false, 400)
{
}
}
This is part of my Register method in Users Controller.
[HttpPost]
public async Task<IActionResult> Register(UserRegisterInputModel input)
{
if (!ModelState.IsValid)
{
return Json(new BadResponseModel()
{
ErrorMessages = new List<string>()
{
"Invalid register information"
}
});
}
ApiController functionality auto-validates my model and the return statement for BadResponseModel is never reached. Is there any way of stopping auto-validation or changing the default response from ApiController validation ?