I have a sample controller when called should return a serialised object with a list of Animals with all there properties dislayed. The trouble im facing is that its only serialising the property in the base class
Model
public class MyAnimals
{
public string Description { get; set; }
public ICollection<Animal> Animals { get; set; }
}
public class Animal
{
public string Name { set; get; }
}
public class Dog: Animal
{
public string Says { get; set; }
}
public class Cat : Animal
{
public string Likes { get; set; }
}
Controller
[HttpGet]
public IActionResult Animals()
{
var animals = new MyAnimals()
{
Description = "My favorite animals",
Animals = new List<Animal>()
{
new Cat()
{
Name = "Tom",
Likes = "Cheese"
},
new Dog()
{
Name = "Pluto",
Says = "Bark"
}
}
};
return Ok(animals);
}
when the object is seralised its coming out like this
Current Response
{
"description": "My favorite animals",
"animals": [
{
"name": "Tom"
},
{
"name": "Pluto"
}
]
}
The required output im after should be like the below. Please tell me what im doing wrong
Required Response
{
"description": "My favorite animals",
"animals": [
{
"name": "Tom",
"Likes": "Cheese"
},
{
"name": "Pluto",
"Says": "Bark"
}
]
}