Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent C# from wrapping my JSON output in my test controller?

New to Visual Studio and C#. I'm working on the UI for a .Net Core/MVC React-Redux app (someone else is working on the model and controllers) and as part of development I wanted to wire up a test controller to output some JSON that I am reading from a text file.

It's working except that C# is wrapping my controller output JSON in another set of brackets which is causing problems consuming the JSON in React.

My test controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace AGDemo.Controllers
{
    [Produces("application/json")]
    [Route("api/Versions")]
    public class VersionsController : Controller
    {
        // GET: api/Versions
        [HttpGet]
        public IEnumerable<JsonResult> Get()
        {
            var data = System.IO.File.ReadAllText(@"..\AGDemo\Api\JSONVersionData.txt");
            var dataObj = JsonConvert.DeserializeObject(data);
            Console.WriteLine(dataObj)
            yield return Json(dataObj);
        }

    }
}

The console output:

[
  {
    "VersionCode": "0ODD6109 ",
    "VersionQualifier": " ",
    "VersionID": "a74bb26c-c23f-e711-80da-0050568f4ab2",
    "ProductID": "WRAP ",
    "PieceName": "HOME ACCENTS ",
    "PrintClass": "WRAP ",
    "FirstInhomeDate": "2017-12-12 00:00:00.000",
    "AccountID": "103448",
    "AccountExecutive": "AMANDA COOK (A291)",
    "AccountManager": "OLIVIA DOWD (13406)",
    "ArtManager": "OLIVIA DOWD (13406)",
    "AdUID": "",
    "Status": "",
    "Queue": "",
    "DueDateOverride": "",
    "IsLocked": ""
  },
  {
    "VersionCode": "0ODD6110 ",
    "VersionQualifier": " ",
    "VersionID": "6ad828e1-c23f-e711-80da-0050568f4ab2",
    "ProductID": "WRPBAN-IN ",
    "PieceName": "HOME ACCENTS BANNER ",
    "PrintClass": "WRAP ",
    "FirstInhomeDate": "2017-12-12 00:00:00.000",
    "AccountID": "103448 ",
    "AccountExecutive": "AMANDA COOK (A291)",
    "AccountManager": "OLIVIA DOWD (13406)",
    "ArtManager": "OLIVIA DOWD (13406)",
    "AdUID": "",
    "Status": "",
    "Queue": "",
    "DueDateOverride": "",
    "IsLocked": ""
  }
]

The controller output:

[{"contentType":null,"serializerSettings":null,"statusCode":null,"value":[{"VersionCode":"0ODD6109 ","VersionQualifier":" ","VersionID":"a74bb26c-c23f-e711-80da-0050568f4ab2","ProductID":"WRAP ","PieceName":"HOME ACCENTS ","PrintClass":"WRAP ","FirstInhomeDate":"2017-12-12 00:00:00.000","AccountID":"103448","AccountExecutive":"AMANDA COOK (A291)","AccountManager":"OLIVIA DOWD (13406)","ArtManager":"OLIVIA DOWD (13406)","AdUID":"","Status":"","Queue":"","DueDateOverride":"","IsLocked":""},{"VersionCode":"0ODD6110 ","VersionQualifier":" ","VersionID":"6ad828e1-c23f-e711-80da-0050568f4ab2","ProductID":"WRPBAN-IN ","PieceName":"HOME ACCENTS BANNER ","PrintClass":"WRAP ","FirstInhomeDate":"2017-12-12 00:00:00.000","AccountID":"103448 ","AccountExecutive":"AMANDA COOK (A291)","AccountManager":"OLIVIA DOWD (13406)","ArtManager":"OLIVIA DOWD (13406)","AdUID":"","Status":"","Queue":"","DueDateOverride":"","IsLocked":""}]}]

The output I'm looking for (which should be the same as the console output):

[{"VersionCode":"0ODD6109 ","VersionQualifier":" ","VersionID":"a74bb26c-c23f-e711-80da-0050568f4ab2","ProductID":"WRAP ","PieceName":"HOME ACCENTS ","PrintClass":"WRAP ","FirstInhomeDate":"2017-12-12 00:00:00.000","AccountID":"103448","AccountExecutive":"AMANDA COOK (A291)","AccountManager":"OLIVIA DOWD (13406)","ArtManager":"OLIVIA DOWD (13406)","AdUID":"","Status":"","Queue":"","DueDateOverride":"","IsLocked":""},{"VersionCode":"0ODD6110 ","VersionQualifier":" ","VersionID":"6ad828e1-c23f-e711-80da-0050568f4ab2","ProductID":"WRPBAN-IN ","PieceName":"HOME ACCENTS BANNER ","PrintClass":"WRAP ","FirstInhomeDate":"2017-12-12 00:00:00.000","AccountID":"103448 ","AccountExecutive":"AMANDA COOK (A291)","AccountManager":"OLIVIA DOWD (13406)","ArtManager":"OLIVIA DOWD (13406)","AdUID":"","Status":"","Queue":"","DueDateOverride":"","IsLocked":""}]

I have tried testing different controller action types. The result is either what I have shown OR if the action result was set to "Object" my JSON was wrapped in [] without the "contentType", "serializerSettings", etc. params.

I know I could just open the JSON text file directly in React-Redux in my action function but ultimately the UI will integrate with C# controllers and I would prefer to call a controller to achieve this.

Any ideas on what I need to change?

like image 312
CHY Avatar asked Sep 12 '25 09:09

CHY


1 Answers

You need to return JsonResult, not IEnumerable<JsonResult>.

This will tell MVC to process that as an ActionResult, instead of an arbitrary object that should be serialized as JSON.

Or change it to return object and return the object directly instead of using a result.

like image 114
SLaks Avatar answered Sep 14 '25 00:09

SLaks