0

I'm create a RESTful API using ASP.NET Web API. I am trying to work out the best way of limiting the amount of data child objects show.

To explain, I have three entities, Objects, Channels and ChannelData. An object can have 0 or more Channels and a Channel can have 0 or more ChannelData (and usually 1000s).

I want the following endpoints:

  • /api/Objects

    • Returns the JSON list of objects and their channels but not the ChannelData (maybe summary data instead e.g. the amount of data etc).
  • /api/Objects/{{objectId}}/ChannelData?channel={{channelName}}

    • Returns all the data in JSON for a particular channel for a particular object.

By default, Web API tries to show everything in /api/Objects including 1000s of ChannelData objects. What is the best way of preventing the array of ChannelData from appearing for a specific endpoint?

Mat
  • 140
  • 2
  • 11
  • 1
    Web API only shows what you return in the method. So, don't load channeldata? – Andy Wiesendanger Mar 09 '16 at 14:49
  • 1
    Are you using an ORM? Can you post the code for your `GET` method? – Michael Mar 09 '16 at 14:55
  • Object contains a list of Channels which contain a list of ChannelData. If I return an Object it will automatically include the Channels list and the ChannelData list. – Mat Mar 09 '16 at 15:06

1 Answers1

0

Actually the best way to achieve this is to "not load the data"; you can also consider to change the response of you method with a different model.

Anyway, if you can't do anything of that, you can try using this tag [JsonIgnore] on ChannelData property (or [IgnoreDataMember] if you use the xml formatter); the serialization engine will skip that property during response generation.

Check this article or this article for more details or this SO answer.

Luca Ghersi
  • 3,261
  • 18
  • 32