Simply Restful Routing
Note - this component is deprecated SimplyRestfulRouting has not been maintained for several years, and as such will not be included in builds of MvcContrib going forward.
The
SimplyRestfulRouteHandler allows you to easily create a set of default routes for your application using a restful style for the urls.
General Concepts
REST, as defined by wikipedia, is a pattern for defining and addressing resources on networks like the web. Basically it gives easy, clear, and consistent urls and can actually save you some bandwidth and saves on the number of requests your webserver has to handle.
If you are unfamiliar with the basic idea of routing, check out some of the
external resources before continuing. It is important that you understand the basics of how controllers and actions are mapped and views are rendered.
Restful routing defines 8 actions, rails uses 7, but we added an extra
delete action so you can be build custom forms to either validate some input or confirm a deletion. The 8 actions can be split into two groups. Read actions or nondestructive actions, and write actions or actions with side effects. You will also notice that the actions have a very close relationship to the CRUD concept of Create, Read, Update, Delete.
The Eight Actions Action | HTTP Method | Default Url | Description |
Index | GET | [controller] | Gets a collection of resources |
Show | GET | [controller]/[id] | Gets a single resource identified by id |
Edit | GET | [controller]/[id]/edit | Gets a form to edit resources identified by id |
Delete | GET | [controller]/[id]/delete | Gets a form to confirm a delete of the resource identified by id |
New | GET | [controller]/new | Gets form to a create a new resource |
Create | POST | [controller] | Creates the new resource |
Update | PUT | [controller]/[id] | Updates the resource identified by id |
Destroy | DELETE | [controller]/[id] | Deletes the resource identified by id |
The above table shows that we map several actions to the same url, how do we do that? Notice that the HTTP Method is different for each url that is the same. When constructing the routes in MVC we use validation filters on the
Method parameter of the route to map the request to the correct action.
This has one side effect, some browsers do not support PUT and DELETE requests. So instead of using javascript hacks we use a simpl form _method hack and the SimplyRestfulRouteHandler. The form method hack requires that regular HTTP POST request get sent and included in the submitted form a field named "_method" with a value of the intended restful http method. So adding that the routing table now looks like this.
Action | HTTP Method | Form Method | Default Url | Handler |
Index | GET | | [controller] | MvcRouteHandler |
Show | GET | | [controller]/[id] | MvcRouteHandler |
Edit | GET | | [controller]/[id]/edit | MvcRouteHandler |
Delete | GET | | [controller]/[id]/delete | MvcRouteHandler |
New | GET | | [controller]/new | MvcRouteHandler |
Create | POST | | [controller] | MvcRouteHandler |
Update | PUT | | [controller]/[id] | MvcRouteHandler |
Update | POST | PUT | [controller]/[id] | SimplyRestfulRouteHandler |
Destroy | DELETE | | [controller]/[id] | MvcRouteHandler |
Destroy | POST | DELETE | [controller]/[id] | SimplyRestfulRouteHandler |
Getting Started
Default Routes
Routes with Areas
Advanced Routes