© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
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;
}
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"
  }]
}

```
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

List as a field of an object. [Answered]
C#CC# / help
4y ago
✅ ToString of a list of an object
C#CC# / help
3y ago
❔ ASP.NET MVC: Object reference not set to an instance of an object
C#CC# / help
3y ago
❔ list patterns net 7
C#CC# / help
4y ago