I’m using ASP.NET Web API and I’m trying to return an instance of a class that looks like this:
public class Whatever
{
public int MyInt {get; set;}
public IFoo MyFoo {get; set;}
}
When this instance gets serialized to JSON, the IFoo member (which could be an instance of any of a number of classes implementing IFoo) gets serialized oddly. The property name is written, but its value is a huge chunk of HTML containing an error message like this:
You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Is this the framework telling me that it doesn’t know how to serialize an instance cast as an interface? Or something else?
It appears that this error occurs whenever the framework tries to serialize a member whose type is an interface. Apart from changing the member so that its type is concrete, I can find no other way around this problem.