I have a paginated lookup to which I decided to add a total count, so that the front-end webpage can get the total (non-paginated) count and the paginated list in one API call. To do this, I created a wrapper class to contain the previously working List and the total count:
public class ReturnList<T>
{
public List<T> list;
public int unpaginatedLength;
public ReturnList(List<T> list, int unpaginatedLength)
{
this.list = list;
this.unpaginatedLength = unpaginatedLength;
}
}
Then in my get:
[HttpGet("get")]
public async Task<ActionResult<ReturnList<dynamic>>> Get(params)
{
List<dynamic> myList = await GetListSomehow(); // Using dynamic because I'm specifying columns to return
int totalLength = await GetLengthSomehow();
ReturnList returnList = new ReturnList<dynamic>(myList, totalLength);
return Ok(returnList);
}
When I inspect everything in the debugger, everything looks like I expect it (returnList
has a List
(or an array, when I tried swapping out the types) with all the right objects, and an int
with the expected length) to look up until it returns, but when I receive the reply in the front-end, the body of the response is just an empty {}
. Is there something I'm missing to get the values to return across the internet correctly?
CodePudding user response:
Try changing the fields inside of ReturnList to properties.
public class ReturnList<T>
{
public List<T> list { get; set; }
public int unpaginatedLength { get; set; }
}
I believe the json serializer only serializes properties, while ignoring fields.