C#C
C#3y ago
Vortac

✅ ASP Net 7 - Return list of objects as an object with a nested list

I have an endpoint with the following code:

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"
  }
]

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"
  }]
}

```
Was this page helpful?