In context of a dotnet core REST api, I want to return a 'light' version of objects when listing them to avoid a lot of unacessary data. I try to use polymorphism to do so :
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Student : Person
{
public int Age { get; set; }
}
[HttpGet("testList")]
public List<Person> getPersons()
{
List<Person> testList = new List<Person>();
testList.Add(new Student() { FirstName = "Kevin", LastName = "Smith", Age = 99 });
return testList;
}
[HttpGet("test")]
public Student getStudent()
{
return new Student() { FirstName = "Kevin", LastName = "Smith", Age = 99 };
}
When I call /testList, I got the following json :
[
{
"age": 99,
"firstName": "Kevin",
"lastName": "Smith"
}
]
I was expecting return to not contain age parameter due to return type, but it's not the case.
Is there a way to get a 'light version' only for the listing method ?
Thank you,