Restful Routing with ASP MVC Preview 2

There is still very little documentation available for ASP MVC 2, and I had a bit of trouble finding out how to put HTTP method constraints on routes. After a bit of poking around I found out that the Method value was changed to httpMethod. I generally follow the convention that my controller is named the same as my resource. For instance, a resource named “Rates” would have a Rates controller. I use this little utility method to map the REST routes for the Rates resource.

public class GlobalApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        MapResource(routes, "Rates");
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    protected static void MapResource(RouteCollection routes, string resourceName)
    {

        routes.Add(new Route(resourceName, new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(
                new { controller = resourceName, action = "List" }),
            Constraints = new RouteValueDictionary(
                new { httpMethod = "GET" }),
        });

        routes.Add(new Route(resourceName, new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(
                new { controller = resourceName, action = "Create" }),
            Constraints = new RouteValueDictionary(
                new { httpMethod = "PUT" }),
        });

        routes.Add(new Route(resourceName + "/{id}", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(
                new { controller = resourceName, action = "Read" }),
            Constraints = new RouteValueDictionary(
                new { httpMethod = "GET" }),
        });

        routes.Add(new Route(resourceName + "/{id}", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(
                new { controller = resourceName, action = "Update" }),
            Constraints = new RouteValueDictionary(
                new { httpMethod = "POST" }),
        });

        routes.Add(new Route(resourceName + "/{id}", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(
                new { controller = resourceName, action = "Delete" }),
            Constraints = new RouteValueDictionary(
                new { httpMethod = "DELETE" }),
        });
    }
}

It’s pretty basic, but it keeps your code DRY and saves time if you’re doing simple REST routes for a few different models.

This entry was posted on Tue, 15 Apr 2008 21:30:00 GMT and Posted in , . You can follow any any response to this entry through the Atom feed. You can leave a comments, Or a trackback from your own site.
Tags , ,


Trackbacks

Use the following link to trackback from your own site:
http://www.sshconnection.com/trackbacks?article_id=restful-routing-with-asp-mvc-preview-2&day=15&month=04&year=2008

  1. Soma. From Soma.
    Soma.
  2. Temp soma. Aura soma bottles. Cheap soma watson. Soma to florida. Soma san diego.
  3. Soma. From Soma.
    Soma.

Comments

Leave a response

  1. Bernie Mcmahon about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.roadracingworld.com/ >Roadracing World http://www.topix.com/city/gasport-ny

  2. Judy Bell about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.erowid.org/psychoactives/faqs/faq_natural_high_australia.shtml >Australian Natural Highs FAQ http://cnn.com/ASIANOW/asiaweek/current.bak/issue/biz3b.html

  3. Juliana Gordon about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://exit3.i-55.com/~moonhorn/ >Moonhorn Mosaics http://www.ucdsm.org/

  4. Tony Fletcher about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.sylvaniaheaters.com/ >Sylvania Heaters http://www.union-securities.com/

  5. Kirsten Bryant about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://cnn.com/2003/US/11/28/sprj.irq.dreaded.visit.ap/ >How families learn of military deaths in Iraq http://www.dechen.org

  6. Renita Lamb about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.sittercentral.com/page/page/507357.htm >SitterCentral http://www.pctechguide.com/22Pentium.htm

  7. Chris Guidry about 1 month later:

    Two suggestions:

    1. Enable spam filtering on your posts.
    2. PUT to a collection resource usually doesn’t mean Create a new member

    Other than that, great job! And I love you, Scottster!

  8. Sandra Soto about 1 month later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://cnn.com/2002/US/04/11/military.warning/index.html >Official: U.S. military, families in Mideast get security caution http://study.haifa.ac.il/~ssalma04/

  9. Lionel Nguyen 2 months later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.taveuniislandfiji.com >Taveuni Island Fiji Resort Guide http://cnn.com/2001/SHOWBIZ/TV/07/16/west.wing.reut/

  10. Drew Fowler 2 months later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.tradebuilders.com/ >TradeBuilders http://www.geocities.com/CapeCanaveral/4742/snail_faq.html

  11. Dianna Lowe 2 months later:

    korymbos cateye repetitory neuropath diethylenediamine pervadingly americanoid betag http://www.famed-zywiec.com/ >Famed http://www.wolfweb.com.au/bp/

Leave a comment