Thanks to the guys who are infinitely smarter than me that did MVCContrib, adding RESTful behavior (using SimplyRestfulRouteHandler) to a .NET MVC app is pretty simple.
- Download the MVCContrib libraries (I downloaded the source because I like to break stuff)
- Open up your ASP .NET MVC application and add a reference to the MVCContrib.dll
- Open up the Global.asax.cs and add the following line inside the ResgisterRoutes method:
MvcContrib.SimplyRestful.SimplyRestfulRouteHandler.BuildRoutes(routes);
Action | URL | Http | Form |
Show | [controller]/[id] | GET | |
Create | [controller] | POST | |
Update | [controller]/[id] | PUT | |
Update | [controller]/[id] | POST | PUT |
Destroy | [controller]/[id] | DELETE | |
Destroy | [controller]/[id] | POST | DELETE |
Index | [controller] | GET | |
New | [controller]/new | GET | |
Edit | [controller]/[id]/edit | GET | |
Delete | [controller]/[id]/delete | GET |
HTTP verb: [GET] (for show)
URI: https://api.mydomain.com/v1/People/1234
HTTP verb: [PUT] (for update)
URI: https://api.mydomain.com/v1/People/1234
HTTP verb: [POST] (for create)
URI: https://api.mydomain.com/v1/People/
so on...
So what if you wanted to address hierarchy with the object using the same URI scheme? Let's say you wanted to get all of the addresses for a particular person and addresses were a child of person:
HTTP verb: [GET]
URI: https://api.mydomain.com/v1/People/1234/addresses
Thanks to the slick implementation of the RouteHandler all you have to do is add another call to BuildRoutes using an overload that accepts an "areaPrefix"
SimplyRestfulRouteHandler.BuildRoutes(routes, "People/{individualID}");
So now you can use:https://api.mydomain.com/v1/People/1/addresses
<-- collectionhttps://api.mydomain.com/v1/People/1/addresses/3
<-- singlehttps://api.mydomain.com/v1/addresses/3
<--single no context
No comments:
Post a Comment