-1

I want to serialize a derived class and send it to a web-api method. In my WebApiConfig.cs I have set

config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;

I have set the $type parameter in my JSON (or JavaScript which will be serialized to JSON) but the derived class still doesn't get deserialized correctly (always null).

Timothy
  • 608
  • 3
  • 10
  • 24

1 Answers1

2

This could be because you don't set the $type parameter as your first property. For example this will work:

var param = {
    $type: 'MyNamespace.MyType, MyAssemblyName', // .NET fully qualified name
    name: 'test' // object properties
};

but this won't work:

var param = {
    name: 'test' // object properties
    $type: 'MyNamespace.MyType, MyAssemblyName', // .NET fully qualified name
};
Timothy
  • 608
  • 3
  • 10
  • 24
  • 1
    If this is happening, @Timothy can set [`JsonSerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead`](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_MetadataPropertyHandling.htm). See [Newtonsoft JSON.net deserialization error where fields in JSON change order](https://stackoverflow.com/questions/29493292/newtonsoft-json-net-deserialization-error-where-fields-in-json-change-order/29526269#29526269). – dbc Sep 01 '15 at 00:03