C# .NET: Microsoft MVC Route Examples

12/17/2013 4:38:42 PM

Examples of routes than can be added to an MVC .NET website.

//Subitem Id Details = ex: /articles/1232-the-name-of-my-article/ -> where the last parameter starts with a number
routes.MapRoute(
	"Subitem Id Details",
	"{controller}/{id}/{itemId}",
	new { controller = "Home", action = "Details" },
	new { id = @"^\d(.*)$" },
	new[] { "MyWebsite.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;

//Subitem Url Title Details
routes.MapRoute(
	"Subitem Url Title Details",
	"{controller}/{id}/{itemId}",
	new { controller = "Home", action = "Details" },
	new { id = @"^\d(.*)$" },
	new[] { "MyWebsite.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;


//Default confined by namespace
routes.MapRoute(
	"Default", // Route name
	"{controller}/{action}/{id}", // URL with parameters
	new { controller = "Home", action = "Index", id = UrlParameter.Optional },
	new[] { "MyWebsite.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;

//Default - not confined by namespace
routes.MapRoute(
	"Default", // Route name
	"{controller}/{action}/{id}", // URL with parameters
	new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens["UseNamespaceFallback"] = false;

//Default
routes.MapRoute(
	"Default", // Route name
	"{controller}/{action}/{id}", // URL with parameters
	new { controller = "Home", action = "Index", id = UrlParameter.Optional }	
)