✅ ASP Net 7 - Return list of objects as an object with a nested list
I have an endpoint with the following code:
This returns a list like:
I want to return:
```
public async Task<List<Page>> ListPages()
{
var pages = await _dbContext.Pages.ToListAsync();
return pages;
}public async Task<List<Page>> ListPages()
{
var pages = await _dbContext.Pages.ToListAsync();
return pages;
}This returns a list like:
[
{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}
][
{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}
]I want to return:
```
{ "data":
[{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}]
}```
{ "data":
[{
"id": 1,
"title": "Hello ASP!",
"body": "<p>Testing</p>",
"author": "M"
},
{
"id": 2,
"title": "Second Page",
"body": "<p>Cool!</p>",
"author": "M"
}]
}```