Reputation: 608
Recently we have migrated our project from .NET Framework to .NET Core 3.1.
Suppose we have a property defined as bool
, it's not accepting values as null and getting the following error when we try to call API from UI
Error converting value {null} to type 'System.Boolean'
Tried adding this in start up to fix the issue
services.AddControllers().AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
Upvotes: 1
Views: 3544
Reputation: 608
I have modified the start up and it worked for me. Thanks everyone for your suggestions
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.UseMemberCasing();
//This will help us in accepting null values to other properties types
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
Upvotes: 3