I am working on a test ASP.NET Core 2.0
service to expand my programming knowledge to include server side experience. I have a Registration Model class that is sent to the service when a user is registering in my application. My RegistrationModel
class is below:
public class RegistrationModel
{
[Required]
public string Username { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
The RegisterUser
method I am calling is below. I am POSTing the data to the service and retrieving it from the Body
of the call.
[HttpPost(nameof(RegisterUser))]
[AllowAnonymous]
public async Task<IActionResult> RegisterUser([FromBody] RegistrationModel registrationInfo)
When sending the JSON to the service, the ModelState.IsValid is always true unless I send a null object. If I send a completely empty object, the ModelState.IsValid is still true. I've also tried the to remove the [FromBody]
from the parameter list for the method and that didn't change anything.
Is there some option that needs to be set in the startup.cs file to validate the models automatically or is there manual validation I need to perform myself? If I need to provide any other information, just let me know.