Using ASP.NET Core web API framework, my controller returns a list, which currently formats its output in JSON as a plain array. How do I wrap an object around this so it outputs as an object with an array nested inside of it instead?
I know that I can do this by creating a new class to hold this list as a property, but I wanted to know if there is a cleaner way that I can do this code block (especially since the class would only be used for this method).
My controller code is as follows:
public async Task<IActionResult> GetIntranets(string utilityServer,
string configuration) {
List<Intranet> intranets =
await _intranetService.GetIntranets(utilityServer, configuration);
return new OkObjectResult(intranets);
}
Current output:
[
{
"name": "Intranet 1",
"taskmanPath": "\\\\hostname\\syteline",
"reportPreviewUrl": "https://cname.domain.com/slreports",
"reportServerUrl": "http://hostname/ReportServer_XYZ_TEST",
"masterSite": "DALS"
},
{
"name": "Intranet 2",
"taskmanPath": "\\\\hostname\\syteline",
"reportPreviewUrl": "https://cname.domain.com/slreports",
"reportServerUrl": "http://fqdn/ReportServer_XYZ_PILOT",
"masterSite": "DALS"
},
]
Desired output:
{
"Intranets": [
{
"name": "Intranet 1",
"taskmanPath": "\\\\hostname\\syteline",
"reportPreviewUrl": "https://cname.domain.com/slreports",
"reportServerUrl": "http://hostname/ReportServer_XYZ_TEST",
"masterSite": "DALS"
},
{
"name": "Intranet 2",
"taskmanPath": "\\\\hostname\\syteline",
"reportPreviewUrl": "https://cname.domain.com/slreports",
"reportServerUrl": "http://fqdn/ReportServer_XYZ_PILOT",
"masterSite": "DALS"
}
]
}