0

I am trying to use an HTTP Get request to pull an array from the C# WebAPI

I used the following code to get the list

        [HttpGet]
        [Route("api/Events/GetEvents")]
        public List<Event> GetEvents()
        {
            db.Configuration.ProxyCreationEnabled = false;
            List<Event> getevents = db.Events.ToList();
            return getevents;
            
        }

this is how the the JSON formatter used in Global.asx

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
                GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

but this is the array that I get

{"$id":"1","$values":[{"$id":"2","Event_id":1,"Event_Name":"Soccer","Event_Date":"2021-12-02T00:00:00","Fixtures":{"$id":"3","$values":[]}},{"$id":"4","Event_id":2,"Event_Name":"Rugby","Event_Date":"2021-12-03T00:00:00","Fixtures":{"$id":"5","$values":[]}}]}

Anybody know how I can fix this?

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
  • 1
    Welcome to StackOverflow. The reason you're getting weird serialization is because of how you've configured reference loop handling on your serializer settings. JSON doesn't have a built-in way to represent references, and Angular doesn't respect JSON.NET's way to handle those. You probably need to set up your object model so it doesn't require reference handling (i.e. no circular references, and no two references to the same object). Then remove that customization. – StriplingWarrior Sep 20 '21 at 19:08
  • Do [AngularJS, MVC4, Web API, Json Deserialize $ref](https://stackoverflow.com/q/14288616/3744182) or [Resolve circular references from JSON object](https://stackoverflow.com/q/15312529/3744182) answer your question? – dbc Sep 20 '21 at 23:37

0 Answers0