Created
June 16, 2010 03:11
-
-
Save jarrettmeyer/440101 to your computer and use it in GitHub Desktop.
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
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace jarrettmeyer.Web.Lib | |
{ | |
public static class RouteHelpers | |
{ | |
/// <summary> | |
/// Connects a distinct path to a controller/action. | |
/// </summary> | |
public static void Connect(this RouteCollection routes, string name, string path, string controller, string action, params string[] methods) | |
{ | |
routes.MapRoute(name, path, new { controller, action, id = "" }, new { method = new HttpMethodConstraint(methods) }); | |
} | |
/// <summary> | |
/// Creates nested resource route bindings. | |
/// </summary> | |
public static void NestedResource(this RouteCollection routes, string name, string parentPath, string childPath, string controller, string parentId) | |
{ | |
routes.MapRoute(name + "_index", parentPath + "/{" + parentId + "}/" + childPath, new { controller, action = "index" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_new", parentPath + "/{" + parentId + "}/" + childPath + "/new", new { controller, action = "new" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_create", parentPath + "/{" + parentId + "}/" + childPath, new { controller, action = "create" }, new { method = new HttpMethodConstraint("post") }); | |
} | |
/// <summary> | |
/// Creates resource route bindings for index, show, new, create, edit, update, and delete operations. | |
/// </summary> | |
public static void Resource(this RouteCollection routes, string name, string basePath, string controller) | |
{ | |
routes.MapRoute(name + "_index", basePath, new { controller, action = "index" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_new", basePath + "/new", new { controller, action = "new" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_create", basePath, new { controller, action = "create" }, new { method = new HttpMethodConstraint("post") }); | |
routes.MapRoute(name + "_show", basePath + "/{id}", new { controller, action = "show", id = "" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_edit", basePath + "/{id}/edit", new { controller, action = "edit", id = "" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_update", basePath + "/{id}", new { controller, action = "update", id = "" }, new { method = new HttpMethodConstraint("post", "put") }); | |
routes.MapRoute(name + "_delete", basePath + "/{id}", new { controller, action = "delete", id = "" }, new { method = new HttpMethodConstraint("delete") }); | |
} | |
/// <summary> | |
/// Connects a distinct path to a controller/action. | |
/// </summary> | |
public static void Connect(this AreaRegistrationContext context, string name, string area, string path, string controller, string action, params string[] methods) | |
{ | |
context.MapRoute(name, area + "/" + path, new { area, controller, action, id = "" }, new { method = new HttpMethodConstraint(methods) }); | |
} | |
/// <summary> | |
/// Creates nested resource route bindings. | |
/// </summary> | |
public static void NestedResource(this AreaRegistrationContext routes, string name, string area, string parentPath, string childPath, string controller, string parentId) | |
{ | |
routes.MapRoute(name + "_index", area + "/" + parentPath + "/{" + parentId + "}/" + childPath, new { area, controller, action = "index" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_new", area + "/" + parentPath + "/{" + parentId + "}/" + childPath + "/new", new { area, controller, action = "new" }, new { method = new HttpMethodConstraint("get") }); | |
routes.MapRoute(name + "_create", area + "/" + parentPath + "/{" + parentId + "}/" + childPath, new { area, controller, action = "create" }, new { method = new HttpMethodConstraint("post") }); | |
} | |
/// <summary> | |
/// Creates resource route bindings for index, show, new, create, edit, update, and delete operations. | |
/// </summary> | |
public static void Resource(this AreaRegistrationContext context, string name, string area, string basePath, string controller) | |
{ | |
context.MapRoute(name + "_index", area + "/" + basePath, new { area, controller, action = "index" }, new { method = new HttpMethodConstraint("get") }); | |
context.MapRoute(name + "_new", area + "/" + basePath + "/new", new { area, controller, action = "new" }, new { method = new HttpMethodConstraint("get") }); | |
context.MapRoute(name + "_create", area + "/" + basePath, new { area, controller, action = "create" }, new { method = new HttpMethodConstraint("post") }); | |
context.MapRoute(name + "_show", area + "/" + basePath + "/{id}", new { area, controller, action = "show", id = "" }, new { method = new HttpMethodConstraint("get") }); | |
context.MapRoute(name + "_edit", area + "/" + basePath + "/{id}/edit", new { area, controller, action = "edit", id = "" }, new { method = new HttpMethodConstraint("get") }); | |
context.MapRoute(name + "_update", area + "/" + basePath + "/{id}", new { area, controller, action = "update", id = "" }, new { method = new HttpMethodConstraint("post", "put") }); | |
context.MapRoute(name + "_delete", area + "/" + basePath + "/{id}", new { area, controller, action = "delete", id = "" }, new { method = new HttpMethodConstraint("delete") }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment