GetCustomers([Fro" /> GetCustomers([Fro" /> GetCustomers([Fro"/>

ASP.NET Core model binding fails for DateTime or DateOnly types

452 Views Asked by At

In my ASP.NET Core 6.0 api project, I have this endpoint in the controller:

[HttpGet("Customers/{DateOfBirth}")]
Task<IActionResult> GetCustomers([FromRoute] DateTime? dateOfBirth, CancellationToken token){....}

I'm calling that endpoint with a refit client like this:

[Get("/Customers/{dateOfBirth}")]
public Task<IEnumerable<Customer>> GetCustomersAsync(DateTime? dateOfBirth, CancellationToken token);

Get the 404 Bad Request response stating model validation has failed.

Refit.ValidationApiException: Response code does not indicate success: 404 (Bad Request).

However, no problem if I use the postman to call the api (.../customers/01-01-1980). I read in another SO post that the problem is the ":" in the time part.

So I tried the new DateOnly type from .NET 6 but this time I get the 415 (Unsupported Media Type).

Refit.ValidationApiException: Response code does not indicate success: 415 (Unsupported Media Type).

I am not sure how postman is able to send the request without any issue. Obviously if I send the date as a string, no issue but I have to convert it back into DateTime in the controller which I want to avoid. So is this issue specific to Refit or any HttpClient request? I'm aware Refit is using HttpClient behind the scene.

0

There are 0 best solutions below