Click here to Skip to main content
15,886,067 members
Articles / Web Development / HTML

RESTful routing in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
26 May 2008MPL3 min read 43.5K   259   19   2
This sample application demonstrates how to use the SimplyRestfulRouteHandler to provide a RESTful approach to client-server communication.

Introduction

This sample application demonstrates how to use the SimplyRestfulRouteHandler to provide a RESTful approach to client-server communication. The SimplyRestfulRouteHandler is included in the MVC Contrib project, which extends the functionality of the ASP.NET MVC framework.

Background

MvcContrib is packed with quite a few gems. One of these is the SimplyRestfulRouteHandler, a route utility created by Adam Tybor. Using the SimplyRestfulRouteHandler, the following 10 Routes are assigned to the 8 Actions below.

ActionURLHTTP MethodForm Method
Show[controller]/[id]GET
Create[controller]POST
Update[controller]/[id]PUT
Update[controller]/[id]POSTPUT
Destroy[controller]/[id]DELETE
Destroy[controller]/[id]POSTDELETE
Index[controller]GET
New[controller]/newGET
Edit[controller]/[id]/editGET
Delete[controller]/[id]/deleteGET

* Based on information from Adam Tybor's site.

The route handler is surprisingly easy to use, but it can be tricky to set up if you are not familiar with the new method signatures of the latest MVC source code refresh. I created this sample application based on the MVC HomeController that highlights the 8 Actions defined by SimplyRestfulRouteHandler. To follow along, you will need the 4/16 MVC source code refresh (build 0416) and the 4/19 release of the MvcContrib library (0.0.1.101).

Using the Code

First, you should create a new 'ASP.NET MVC Web Application' project from the 'My Templates' portion of the 'New Project' dialog. If you use the template of the same name under the 'Visual Studio installed templates' portion, you will be using the latest official release of MVC and not the source code refresh. In the global.asax.cs file, replace the RegisterRoutes method with the following:

C#
public static void RegisterRoutes(RouteCollection routes)
{
  SimplyRestfulRouteHandler.BuildRoutes(routes);
}

This will allow the route handler to build all 10 routes for you, based on templates listed in the table above.

Next, we open the HomeController.cs file and add the corresponding actions.

C#
public ActionResult Show(string id)
{
    ViewData["Title"] = "Show";
    ViewData["Message"] = "This will <em>Show</em> resource " + id;

    return RenderView("Index");
}

public ActionResult Create()
{
    ViewData["Title"] = "Create";
    ViewData["Message"] = "This will <em>Create</em> a new resource";

    return RenderView("Index");
}

public ActionResult Update(string id)
{
    ViewData["Title"] = "Update";
    ViewData["Message"] = "This will <em>Update</em> resource " + id;

    return RenderView("Index");
}

public ActionResult Destroy(string id)
{
    ViewData["Title"] = "Destroy";
    ViewData["Message"] = "This will <em>Destroy</em> resource " + id;

    return RenderView("Index");
}

public ActionResult Index()
{
    ViewData["Title"] = "Index";
    ViewData["Message"] = "This is the <em>Index</em>";

    return RenderView("Index");
}

public ActionResult New()
{
    ViewData["Title"] = "New";
    ViewData["Message"] = "This will create a <em>New</em> resource";

    return RenderView("Index");
}

public ActionResult Edit(string id)
{
    ViewData["Title"] = "Edit";
    ViewData["Message"] = "This will <em>Edit</em> resource " + id;

    return RenderView("Index");
}

public ActionResult Delete(string id)
{
    ViewData["Title"] = "Delete";
    ViewData["Message"] = "This will <em>Delete</em> resource " + id;

    return RenderView("Index");
}

For this sample app, all we really want to do is simply display a brief message letting us know which action the user wanted to take. The generated 'Index.aspx' view is fine for this, so we can set the viewName parameter of the RenderView() method to "Index" for all actions, as shown above:

Now, we have just about everything we need. Let's move on to the 'Site.Master' file and enable the user to generate all 8 Actions via click events.

HTML
<ul id="menu">
    <li> <%= Html.ActionLink("Show GET", "Show",  "Home", new { @id="1" }) %> </li>
    <li> <%= Html.ActionLink("Index GET", "Index", "Home")%> </li>
    <li> <%= Html.ActionLink("New GET", "New", "Home")%> </li>
    <li> <%= Html.ActionLink("Edit GET", "Edit", "Home", new { @id = "1" })%> </li>
    <li> <%= Html.ActionLink("Delete GET", "Delete", "Home", new { @id = "1" })%> </li>
    <li> 
        <form action="<%= Url.Action("Create", "Home") %>" method="post" > 
            <a onclick="parentNode.submit();">Create POST</a>
        </form>
    </li>
    <li> 
        <form action="<%= Url.Action( "Update", "Home", new { @id = "1" }) %>" 
              method="post" > 
            <input type="hidden" name="_method" value="put" />
            <a onclick="parentNode.submit();">Update POST</a>
        </form>
    </li>
    <li> 
        <form action="<%= Url.Action( "Destroy", "Home", new { @id = "1" }) %>" 
              method="post" > 
            <input type="hidden" name="_method" value="delete" />
            <a onclick="parentNode.submit();">Destroy POST</a>
        </form>
    </li>
</ul>

If you were watching closely, you should have noticed that the POST events have a hidden input element named "_method" whose value is an HTTP method (PUT or DELETE). Well, most browsers don't support these two methods, so we sometimes need a clever way of initiating these requests. Adam was kind enough to wire up our Destroy and Update Actions so that they fire when the route manager receives standard HTTP PUT and DELETE methods or when it receives a browser-friendly HTTP POST request with a PUT or DELETE "_method" defined. Next, we need to make the form elements of our menu look pretty, so we add the following to our 'Site.css' file.

CSS
ul#menu li form
{
    display: inline;
    list-style: none;
}

And, that's all there is to it. You should be able to launch the application and click on all of the menu items to generate any of the 8 RESTful actions.

History

  • 05/26/2008: Article created.
  • 05/30/2008: Simplified the source code using the "Index" view name for all Actions.

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)


Written By
CEO Technology Today, LLC
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDoesn't seem to work Pin
ChrisN529-Nov-09 9:48
ChrisN529-Nov-09 9:48 
GeneralAnother approach Pin
Piers Lawson20-Jan-09 12:29
Piers Lawson20-Jan-09 12:29 
Nice article! With the latest versions of MVC there are some features that allow a RESTful web service to be created using built in functionality. However, it still falls short of dealing with everything a web service needs to handle (i.e. multiple representations, catching errors and returning HTTP Error Codes). In my blog http://shouldersofgiants.co.uk/Blog/[^] I've tried to put together a full example in a long running series.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.