I have an API endpoint like https://localhost:5001/myResource?fromdate=22-12-2019
.When I do a Get request the RequestFromDate field in the SearchFilters object is set to “01/01/0001 00:00:00" and not a valid date. The user can ONLY send the date in this format dd-MM-yyyy.How do I fix this problem? Am i missing something in my startup class?. I’m using Newtonsoft (Core3.1)
public class SearchFilters
{
[FromQuery(Name = "fromDate")]
public DateTime FromDate { get; set; }
}
MyController
[HttpGet]
public async Task<IActionResult> GetAsync([FromQuery] SearchFilters searchFilters)
{
..
}
Section in my startup where I setup NewtonSoftJson
services
.AddControllers(options => { options.EnableEndpointRouting = false; })
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.AllowInputFormatterExceptionMessages = true;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
})
.AddJsonApi(opt => opt.AllowInputFormatterExceptionMessages = true);
I tried adding the below but without success
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new MyDateFormatConverter());
}
My Custom Converter
public class MyDateFormatConverter: IsoDateTimeConverter
{
public MyDateFormatConverter()
{
DateTimeFormat = "dd-MM-yyyy";
}
}