Click here to Skip to main content
5,786,882 members and growing! (20,795 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate License: The Mozilla Public License 1.1 (MPL 1.1)

RESTful routing in ASP.NET MVC

By Kevin Ortman

This sample application demonstrates how to use the SimplyRestfulRouteHandler to provide a RESTful approach to client-server communication.
C# (C# 1.0, C# 2.0, C# 3.0, C#), HTML, .NET (.NET, .NET 3.5), ASP.NET, Dev

Posted: 26 May 2008
Updated: 26 May 2008
Views: 6,011
Bookmarked: 10 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 1.20 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 50.0%
3
0 votes, 0.0%
4
1 vote, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 the 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.
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.
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.
<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" who's 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.
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)

About the Author

Kevin Ortman



Occupation: CEO
Company: Technology Today, LLC
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2008
Editor:
Copyright 2008 by Kevin Ortman
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project