0

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";
   }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
O'Neil Tomlinson
  • 702
  • 1
  • 6
  • 28
  • 1
    `[FromQuery]` parameters aren't going to be deserialized using Newtonsoft, which is only for deserializing JSON. `"01/01/0001 00:00:00"` looks like `default(DateTime)` -- what you would get if the parameter wasn't assigned at all. – dbc Mar 05 '21 at 23:15
  • If i dont pass in a value, the [FromQuery] field is set to "01/01/0001 00:00:00". But if I set the param to "01-23-2019" it comes out as "23/01/2019 00:00:00" . I would like to get "23/01/2019 00:00:00" when i set the query string is set to "23-01-2019" – O'Neil Tomlinson Mar 05 '21 at 23:52
  • 1
    Right. But `MyDateFormatConverter` isn't going to do that since it's for JSON not query parameters. I think you need to write a custom model binder, see [Custom DateTime Model Binding in ASP.NET Core Web API](http://www.vickram.me/custom-datetime-model-binding-in-asp-net-core-web-api) or [ASP.NET Core model binder deserilises UTC time string to local time rather than UTC time #11584](https://github.com/dotnet/aspnetcore/issues/11584#issuecomment-506007647) for examples. – dbc Mar 06 '21 at 00:03
  • Maybe [this answer](https://stackoverflow.com/a/46308876/3744182) by [Matt Johnson-Pint](https://stackoverflow.com/users/634824/matt-johnson-pint) to [How to make ASP.Net MVC model binder treat incoming date as UTC?](https://stackoverflow.com/q/10293440/3744182) and/or [Change default format for DateTime parsing in ASP.NET Core](https://stackoverflow.com/q/41642800/3744182) are what you want. – dbc Mar 06 '21 at 01:02

0 Answers0