I'm currently migrating an API from ASP.NET Web API to ASP.NET Core. My old API had an endpoint accepting a list as part of the body:
public IHttpActionResult Post(MyModel model)
{
...
}
public class MyModel
{
public List<SomeObject> SomeObjects { get; set; }
}
I have some consumers of this API that was able to post the following JSON:
POST /post
{
"someObjects": {}
}
Of course, this should have been:
POST /post
{
"someObjects": []
}
but ASP.NET Web API model binding accepted this (setting someObjects to null I guess`).
When doing the same with ASP.NET Core, I get the following error when posting the same JSON to the API:
Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[SomeObject]'
Again, I totally understand why the error is there. I need to be backwards compatible, so can anyone help? I guess I need a custom model binder or something to allow the empty object to be converted into null or an empty list?
JsonSerializerSettingsnewtonsoft.com/json/help/html/…{}as a valid value forList<SomeObject. If it'snullor an empty list in ASP.NET Core, I can handle that in the new version.