Routing in practice

Create routes in a web forms application through using the MapPageRoute(String, String, String) method of the RouteCollection class.

ASP.NET platform

The method spawns a Route object, and then adds the object to the RouteCollection object. The parameters hold specified properties for the Route object, which will be passed to the MapPageRoute method. The sample code below adds a Route object to a project:

protected void Application_Start(object sender, EventArgs e)
{
	RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
	routes.MapPageRoute("",
		"Category/{action}/{categoryName}",
		"~/categoriespage.aspx");
}

In an MVC application, no manual addition of routes is required. Preconfigured routes trigger action methods implemented in the controller classes. If a user wants to add custom routes, they employ the MapRoute(RouteCollection, String, String) method, rather than MapPageRoute(String, String, String). The sample code below reveals the code responsible for creating default routes:

public class MvcApplication : System.Web.HttpApplication
{
	public static void RegisterRoutes(RouteCollection routes)
	{
		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
		routes.MapRoute(
			// The name of the route
			"Default",
			// The URL and its parameters
			"{controller}/{action}/{id}",
			// The defaults of the parameters
			new { controller = "Home", action = "Index", id = "" }
		);
	}
	protected void Application_Start()
	{
	RegisterRoutes(RouteTable.Routes);
	}
}
			

In route definition, default values for parameters can be assigned. Routing uses these values in the event of a URL with a missing value. Assign a dictionary object to the Defaults property of the Route class to set values. The sample code below shows how to use the MapPageRoute(String, String, String, Boolean, RouteValueDictionary) method to add a route:

void Application_Start(object sender, EventArgs e)
{
	RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
	routes.MapPageRoute("",
		"Category/{action}/{categoryName}",
		"~/categoriespage.aspx",
		true,
		new RouteValueDictionary
			{{"categoryName", "food"}, {"action", "show"}});
}

In some situations, managing URL requests containing varied numbers of segments may be required. Specify this in the definition to allow for excess segments to process as part of the last segment. Mark the last segment with an asterisk (catch-all parameter), a setting which also matches URLs without values for the last parameter. Review this route pattern for matching an unknown number of segments:

query/{queryname}/{*queryvalues}

Another route specification details requirements for values in the parameters. These constraints prevent URLs with values outside of specifications from using a route in the request. Define constraints with regular expressions or objects that implement the IrouteConstraint interface. Use either a string (for evaluation against a pattern) or an object (for evaluation against the set value). Review the sample code below, which uses the MapPageRoute method to create a route with constraints:

public static void RegisterRoutes(RouteCollection routes)
{
	routes.MapPageRoute("",
		"Category/{action}/{categoryName}",
		"~/categoriespage.aspx",
		true,
		new RouteValueDictionary
			{{"categoryName", "food"}, {"action", "show"}},
		new RouteValueDictionary
			{{"locale", "[a-z]{2}-[a-z]{2}"},{"year", @"\d{4}"}}
		);
}

In some situations, routing does not handle a request despite being enabled; for example, if a physical file exists which matches the URL pattern, or if routing has been explicitly disabled for a pattern.