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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Could I just add option 2 routes to my WebApiConfig route maps?
Sure why not. Here is a good example of that: stackoverflow.com/questions/9594671/… even though I find the attribute routing approach a lot cleaner.
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.

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.