3

I have been looking at routing in the Web.Api and looking at various ways of representing endpoints. I came across Instagrams REST endpoints. (which has some lovely documentation) Using the Web.Api what would be the best way to set up the routing and controllers for a sitution like Instagrams user endpoints?

User Endpoints
GET/users/user-id                  Get basic information about a user.
GET/users/self/feed                See the authenticated user's feed.
GET/users/user-id/media/recent     Get the most recent media published by a user.
GET/users/self/media/liked         See the authenticated user's list of liked media.
GET/users/search                   Search for a user by name.

If I wanted to replicate these endpoints in my app, how would I go about it. Would I just need one controller 'Users' with 5 methods, what kind of routing would I need to direct the REST calls to those methods?

2
  • It's unclear what you are asking. Is this a quiz? Commented Oct 23, 2012 at 23:25
  • @RobertHarvey not a quiz, see edited question. Commented Oct 23, 2012 at 23:38

1 Answer 1

5

I would structure this in a different way.

GET/user
GET/user/user-id
GET/user?q=user-name

GET/media
GET/media/user-id
GET/media/user-id?selection=recent
GET/media/user-id?selection=liked

GET/feed
GET/feed/user-id

This way you keep your Controllers for a specific target, much like keeping your classes for a single responsibility.

  • User
  • Media
  • Feed

When you use this approach it's much easier for a user to 'guess' the path. And I think you could already guess what each path does without any explanation. For me that's the most important when I'm designing a API.

GET/controller            - always returns a item list
GET/controller/id         - always returns a specific item
GET/controller?q=         - always queries the controller
GET/controller?selection= - always selects a subset from the list off items

Ofcourse this is open for interpretation but it gives you an idea about how I would solve this particular problem and maybe some ideas to think about. Also have a look at this great book from Apigee - Web Api Designs

http://info.apigee.com/Portals/62317/docs/web%20api.pdf

Edit:

To make the routes you named I think you've got 2 (not very ideal) options.

  1. Map a specific route for each url
  2. Make a wildcard route

Option 1 I have not tried, or used this myself but you can find more info here:

Single controller with multiple GET methods in ASP.NET Web API

Option 2 If you go the wildcard route all requests with additional parameters will be routed to your default Get() method. In your get you have to look at the parameters using ControllerContext.Request.RequestUri.AbsolutePath or something like it and choose your actions on it.

config.Routes.MapHttpRoute(
     name: "MyApi",
     routeTemplate: "api/{controller}/{id}/{*wildcard}",
     defaults: new { controller = "index", id = RouteParameter.Optional }
 );
Sign up to request clarification or add additional context in comments.

5 Comments

+1 Looks like a good book, thanks for that link. Your clarification of how to structure the api makes sense, I wonder why they didn't do that? However I am still interested in the routing that would be required using the Asp.Net Web.Api to get those routes.
That book is excellent. So nice and simple to read and so clear and defined.
I guess this leads to another question, when you map a specific route to a specific controller action, does the convention (and mapping) GetAction, PostAction, DeleteAction etc. still apply?
@AranMulholland I think so, since you add a HttpMethodConstraint for HttpMethod.Get or Put etc. to the route, but as I said I did never really go this route myself.
Excellent short book. Good suggestion.

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.