Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I am trying to validate an API action method that has a start date and an end date.
public class OrderHistoryRequestDto
{
[Required(ErrorMessage = "Please add StartDate to the request.")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "Please add EndDate to the request.")]
[DataType(DataType.Date)]
public DateTime EndDate { get; set; }
}
I am supposed to get a 400-bad request if I don't send one of the required fields.
But instead, it set the missing date as a default value.
How can I validate this model?
Thank you in advance.
I'm sure Viorel-1 made a typo and is recommending a nullable type.
public class OrderHistoryRequestDto
{
[Required(ErrorMessage = "Please add StartDate to the request.")]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "Please add EndDate to the request.")]
[DataType(DataType.Date)]
public DateTime? EndDate { get; set; }
}
I tested the model with PostMan and an HTTP GET and received the following validation response.
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-13effc30183cfe634ea13672b70e6373-affffa0f4b34e5f1-00",
"errors": {
"EndDate": [
"Please add EndDate to the request."
]
}
}