We have an MVC website that was calling through to WCF Restful services but we are now replacing them with Web Api calls. The problem is that existing code in some of the MVC controllers is relying on the “__type” attribute being returned for each object in the JSON so it can do different things depending on the type of the object.
However, in the new Web Api responses, each of the objects returned don’t have the “__type” entries in the JSON so the code is failing.
Does anyone know how this is done? I’ve found plenty about removing the __type entries for WCF services but nothing yet for making sure they’re in for Web Api.
I found the answer after more Google’ing.
On the WebApi controller project side, in the Global App_Start function I added:
And then on the client C# side when I’m about to deserialized, I added:
So essentially, it’s TypeNameHandling.Objects setting for the serializer which will put out $type for each object instead of __type but it still all works for me.