1

My data structure is fairly simple but I am getting a little confused with how to structure my Web API controllers and routes.

In my data structure a City has a list of Venues and each venue has a list of Events.

This brings me on to the URL routes of the application. Do I have

Option 1

/api/cities/{id} and /api/venues/{id} and /api/events/{id}

But how would that work when POSTing a venue (to a city) or an event (to a venue)?

Option 2

/api/cities/{id} and /api/cities/{cityId}/venues/{id} and /api/cities/{cityId}/venues/{venueId}/events/{id}

Which sort of makes sense because the first call I need to make when using the API is to list all the venues given the city.

Finally am I right in thinking that my API controllers will effectively be getting, upserting and deleting only City documents but will be serving more specific data sets to the client?

Community
  • 1
  • 1
Greg
  • 31,180
  • 18
  • 65
  • 85

1 Answers1

0

I usually favor the second options but that requires either attribute routing package or ASP.NET Web API 2 (not released yet).

First option is also valid as an easier approach, as long as your venue object contains a cityId or event object cotains a venueId.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • Could I just add option 2 routes to my WebApiConfig route maps? – Greg Jul 14 '13 at 14:31
  • Sure why not. Here is a good example of that: http://stackoverflow.com/questions/9594671/nested-resources-in-asp-net-mvc-4-webapi even though I find the attribute routing approach a lot cleaner. – Teoman Soygul Jul 14 '13 at 17:26
  • 1
    I've decided to have collections for each type and add the parent Ids to the child objects, which is easier to maintain than a list of child objects on parents. So in reality its a little bit of both options! The only issue with this is when I delete a parent I must make sure I loop through recursively the children and delete them too. – Greg Jul 16 '13 at 09:00