1

I have a simple http server. I send some json object over POST

At first, I use json converter to parse the json and fill nonserializable objects up

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    list<SomeClass> list = JArray.Load(reader).Cast<JObject>().Select(o => new SomeClass((string)o.GetValue("val1"), (string)o.GetValue("val2"))).ToList();
    return list;
}

The list is loaded fine, but on the next step the field of the class MyClass containing that list is empty

[HttpPost]
public IActionResult PostData([FromBody]MyClass myClass)

Where to look to fix this issue?

cartuhokni
  • 11
  • 1
  • Next step would be to see the actual payload. E.g. install fiddler and then when you run the test see what is in the Request Body is in the fiddler trace. – Thomas Koelle Dec 11 '19 at 07:48
  • @ThomasKoelle the json payload is okay since I get the right data in the list. Also I used Firefox development tools and there was a pretty json object. – cartuhokni Dec 11 '19 at 09:13
  • Ok, if you then add to question how MyClass looks like, and also the Json Body in the call. And then if you explain how MyClass and SomeClass is connected. (Unless someone else understand question better) – Thomas Koelle Dec 11 '19 at 09:44
  • You sure your json object that you created is myClass? (Make sure your JSON object matches with the parameter variable in controller) – Rohan Rao Dec 11 '19 at 15:34
  • For some reason, my services tried to validate the class – cartuhokni Dec 12 '19 at 07:31

1 Answers1

0

The solution was to look on the exception thrown and I could find the reason - validation of object. Solution:

services.AddMvc(options => options.ModelMetadataDetailsProviders.Add(new Microsoft.AspNetCore.Mvc.ModelBinding.SuppressChildValidationMetadataProvider(typeof(MyClass))))
cartuhokni
  • 11
  • 1