I could use "TypeNameHandling = TypeNameHandling.Auto" in previous version of MVC. In MVC6, I have following class
public class BaseClass {
    public string Id {get;set;}
}
public class Foo : BaseClass {
    public string Name {get;set;}
    public string Address {get;set;}
}
public class Bar : BaseClass {
    public string Account {get;set;}
    public string Password {get;set;}
}
In my webapi, JSON result will be the following
[
    {Id: "1", Name: "peter", Address: "address1"},
    {Id: "2", Account: "mary", Password: "1234"}
]
But I want the following result:
[
    {$type: "Foo", Id: "1", Name: "peter", Address: "address1"},
    {$type: "Bar", Id: "2", Account: "mary", Password: "1234"}
]
                You can add new field: type at BaseClass and initialize it at constructor:
public class BaseClass {
    public string Id {get;set;}
    public readonly string type;
    public BaseClass()
    {
        type = this.GetType().Name;
    }
}
At Foo class instances it will be "Foo", at Bar - "Bar".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With