Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The JSON value could not be converted to System.DateOnly with ASP.NET Core 8 Web API

Using .NET 8, I've tried passing in various formats of a date to my API endpoint and I can't get past this error. I get a 400 response with the error:

The JSON value could not be converted to System.DateOnly

My code:

[HttpPost]
public async Task<ActionResult<Item>> CreateItem(CreateItemRequest request)
{
    var Item = await _ItemService.CreateItemAsync(request);

    return Ok(Item);
}

public class CreateItemRequest
{
    ...
    public DateOnly CreateDate { get; set; }
    ...
}

JSON request body:

{
    ...
    "createDate":"2024-1-7"
    ...
}
like image 605
user3953989 Avatar asked Nov 01 '25 19:11

user3953989


1 Answers

The model binding with the DateOnly and TimeOnly is available from .NET 7 as mentioned in Add DateOnly and TimeOnly support to model binding & routing.

The problem is that your provided date string does not match the format. It should be in "yyyy-MM-dd" (ISO 8601) format.

{
    "createDate":"2024-01-07"
}
like image 153
Yong Shun Avatar answered Nov 04 '25 11:11

Yong Shun