I recently upgraded my asp.net core web api to .NET 6. Prior to that, the model binding worked fine.
my request class:
public class FindUserRequest
{
public string EmailAddress { get; set; }
public string Id { get; set; }
public DateTime? DateOfBirth { get; set; }
}
My API controller method:
[HttpPost("FindUser")]
public IActionResult FindUser([FromBody] FindUserRequest request)
{
var response = FindUser(request);
return Ok(response);
}
The JSON being passed in the body:
{
"EmailAddress": "jdoe@example.com",
"Id": "1234",
"DateOfBirth": "03/09/2022"
}
if I pass an ISO formatted date for the DateOfBirth property (e.g. 2022-03-09) everything is fine - but if I pass a date value like 03/09/2022 my request object completely fails to bind and the request parameter is NULL. I tried implementing a custom model binder but I can never seem to get access to the JSON in the request body when I do that. I wish there was a simple 'date format' kind of attribute I could put on the DateOfBirth property but I haven't found anything that seems to work there either. Any Ideas?