When I go to serialize the RageResult object with newtonsoft the serialized string contains only the data from the 'Items' property
Here is the code:
List<string> listString = new List<string>
{
"Test1",
"Test2"
};
var pageResultTest = new PageResult<string>(listString, null, 2);
string serialized = JsonConvert.SerializeObject(pageResultTest, Formatting.Indented);
The pageResult object is serialized in this way:
[
"Test1",
"Test2"
]
The serialization I would expect and want to achieve is this:
{
"Items": [
"Test1",
"Test2"
],
"NextPageLink": null,
"Count": 2
}
PageResult is specifically designed for Razor Page applications to produce an
IActionResultthat renders a page. It's not meant to be used as a data container for serialization purposes. That's why when you try to serialize it directly, you're not getting the expected structure in JSON.pageResult documentation
for the functionality you are after you will need to implement your own custom object that represents the structure you are after:
Sample get api
Than you can seriliaze:
you will get the desired output: