Created
February 1, 2012 07:51
-
-
Save codesoda/1715843 to your computer and use it in GitHub Desktop.
Crud Resoruces Routing for ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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