Handle DateOnly in ASP.NET 6
$ dotnet add package Fonlow.DateOnlyExtensions!For ASP.NET Core 6 only!
Customized serialization for DateOnly in ASP.NET Core 6 and above, introduced in .NET 6, for the following clients:
This component depends on NewtonSoft.Json and derives from Newtonsoft.Json.JsonConverter, and can be injected to ASP.NET Core pipeline through only IServiceCollection.AddNewtonsoftJson().
Usages:
.AddNewtonsoftJson(
options =>
{
options.SerializerSettings.DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset; //Better with this for cross-timezone minValue and .NET Framework clients.
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; //So when controller will ignore null fileds when returing data
options.SerializerSettings.Converters.Add(new DateOnlyJsonConverter()); //not needed for ASP.NET 7 and .NET 7 clients. However .NET 6 clients and .NET Framework clients still need DateOnlyJsonConverter
options.SerializerSettings.Converters.Add(new DateOnlyNullableJsonConverter()); // also, needed by JavaScript clients.
}
);
Remarks:
Hints: