Skip to content

Instantly share code, notes, and snippets.

Created April 3, 2012 12:55
Show Gist options
  • Save anonymous/2291780 to your computer and use it in GitHub Desktop.
Save anonymous/2291780 to your computer and use it in GitHub Desktop.
Convention based routing with MVC
public class DefaultRouteConventions: RouteConventions
{
public DefaultRouteConventions()
{
MapAction("Index").ConstraintToVerb(HttpVerbs.Get);
MapAction("List").ToRootPlural().ConstraintToVerb(HttpVerbs.Get);
MapAction("Details").WithParameters(
new List<ActionParam>() { new ActionParam() { IsComplexType = false, Name = "id" } }
).ToRoot().ConstraintToVerb(HttpVerbs.Get);
MapAction("Delete").WithParameters(
new List<ActionParam>() { new ActionParam() { IsComplexType = false, Name = "id" } }
).ToRoot().ConstraintToVerb(HttpVerbs.Delete);
MapAction("Edit").WithParameters(
new List<ActionParam>() { new ActionParam() { IsComplexType = false, Name = "id" } }
).ToCustomUrl("/edit").ConstraintToVerb(HttpVerbs.Get);
MapAction("Edit").WithParameters(
new List<ActionParam>()
{
new ActionParam() { IsComplexType = false, Name = "id"},
new ActionParam() { IsComplexType = true, Name = "data"}
}
).ToRoot().ConstraintToVerb(HttpVerbs.Put);
MapAction("Create").WithNoParameters().ToCustomUrl("/new").ConstraintToVerb(HttpVerbs.Get);
MapAction("Create").WithParameters(new List<ActionParam>()
{
new ActionParam() { IsComplexType = true, Name = "data"}
}).ToRoot().ConstraintToVerb(HttpVerbs.Post);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment