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?

2
  • 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. Commented Sep 20, 2021 at 19:08
  • Do AngularJS, MVC4, Web API, Json Deserialize $ref or Resolve circular references from JSON object answer your question? Commented Sep 20, 2021 at 23:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.