I had the following converter to serialize the date with a custom format and it was working for .NET Core 3.1:
public class CustomDateConverter : IsoDateTimeConverter {
public CustomDateConverter() {
base.DateTimeFormat = "dd.MM.yyyy";
}
}
public class Test {
[JsonConverter(typeof(CustomDateConverter))]
public DateTime? CustomFormattedDate { get; set; }
}
However, after I migrated to .NET 6.0 the converter no longer gets called. The only way I found was to add the converter to the global list of converters at configuration time, but then it will be used for every DateTime field in my project, which is not what I want. Btw, my configuration looks like this:
builder.Services.AddControllersWithViews()
.AddNewtonsoftJson(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
// options.SerializerSettings.Converters.Add(new CustomDateConverter());
});