I use [EmailAddress] attribute to validate email on server side. However, when I send an invalid email address, I receive a 400 status code response with no message instead of getting into my action method and seeing a ModelState Error.
Debug output simply says that Microsoft.AspNetCore.Mvc.SerializableError is thrown.
Could anyone explain this, please?
Model:
public class LoginVm
{
[Required(ErrorMessage = "Email cannot be empty.")]
[EmailAddress(ErrorMessage = "Email has an incorrect format.")]
public string Email { get; set; }
[Required(ErrorMessage = "Password cannot be empty.")]
public string Password { get; set; }
}
Action:
[AllowAnonymous]
[HttpPost]
public IActionResult Authenticate([FromBody]LoginVm loginVm)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (loginVm.Email != "[email protected]" || loginVm.Password != "password")
{
return NotFound("There is no such user.");
}
return Ok();
}
Debug output:
Request:
POST http://localhost:58072/api/accounts HTTP/1.1
Host: localhost:58072
Connection: keep-alive
Content-Length: 47
Accept: application/json, text/plain, */*
Origin: https://localhost:44381
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Content-Type: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
{"email":"wrongemail","password":"wrongpassword"}
Model Validation in ASP.NET Core from Beginning to Expert. Model Validation is a process to ensure the data received from the View is appropriate to bind the Model. If it is not, then appropriate error messages are displayed on the View, and that will help user to rectify the problem.
This span can be styled with any CSS. It is internally used by ASP.NET Core for displaying validation errors. Additionally, you can add an empty tag (say, a div) to show all the validation errors together at one place. For this the attribute asp-validation-summary="All" must be set as shown.
While we attempt to avoid such incompatibilities, ASP.NET Core 2.2 has been out of support for some time now so it’s difficult for us to capture these kinds of regressions. We would strongly encourage targeting a supported version of ASP.NET Core to ensure continued support.
Server side validation is a pre-check of the POSTED form data before it is processed and passed to a database. Even though client-side validation is sufficient in most cases, yet a second check may be needed on the server side for a confirmed assurance of the sanctity of data that is about to be passed to a database.
The [ApiController]
attribute provides Automatic HTTP 400 responses.
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
Validation errors automatically trigger an HTTP 400 response. The following code becomes unnecessary in your actions:
if (!ModelState.IsValid) { return BadRequest(ModelState); }
How to turn off this feature
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With