Skip to content

Instantly share code, notes, and snippets.

@codesoda
Created February 1, 2012 07:51
Show Gist options
  • Save codesoda/1715843 to your computer and use it in GitHub Desktop.
Save codesoda/1715843 to your computer and use it in GitHub Desktop.
Crud Resoruces Routing for ASP.NET MVC
public static class RouteCollectionExtensions
{
private static void MapResources(this RouteCollection routes, string model)
{
MapResources(routes, model, model + "s");
}
private static void MapResources(this RouteCollection routes, string model, string models)
{
// models - index
routes.MapRoute( "List" + models, models, new { controller = model, action = "Index", id = UrlParameter.Optional } );
// model/new - add
routes.MapRoute( "New " + model, string.Format("{0}/new", model), new { controller = model, action = "Add", id = UrlParameter.Optional });
// model/:id
routes.MapRoute( "View " + model, string.Format("{0}/{{id}}", model), new { controller = model, action = "Show" });
// model/:id/edit
routes.MapRoute("Edit " + model, string.Format("{0}/{{id}}/edit", model), new { controller = model, action = "Edit" });
// model/:id/edit
routes.MapRoute("Destroy " + model, string.Format("{0}/{{id}}/destroy", model), new { controller = model, action = "Destroy" });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment