Routing System In Asp.net MVC3-Incoming URLs
The routing system has two functions:
- Examine an incoming URL and figure out for which controller and action the request is intended
- Generate outgoing URLs. These are the URLs that appear in the HTML rendered from our views so that a specific action will be invoked when the user clicks the link
Routes are defined in Global.asax.cs, which is the code-behind file for Global.asax.
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
The Application_Start method is called by the underlying ASP.NET platform when the application is first started, which leads to the RegisterRoutes method being called.
URL Patterns:
URLs can be broken down into segments. These are the parts of the URL, excluding the hostname and query string, that are separated by the / character.When processing an incoming URL, the job of the routing system is to match the URL to a pattern, and then extract values from the URL for the segment variables defined in the pattern.MVC application will usually have several routes, and the routing system will compare the incoming URL to the URL pattern of each route until it can find a match.
routes.MapRoute("", "Public/{controller}/{action}",new { controller = "Home", action = "Index" });
——This URL pattern will match only URLs that contain three segments, the first of which must be Public. The other two segments can contain any value, and will be used for the controller and action variables.
routes.MapRoute("", "X{controller}/{action}");
——The pattern in this route matches any two-segment URL where the first segment starts with the letter X. The value for controller is taken from the first segment, excluding the X. The action value is taken from the
second segment.
routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
——The route matches any two-segment URL where the first segment is Shop. The action value is taken from the second URL segment. The URL pattern doesn’t contain a variable segment for controller, so the default value we have supplied is used. This means that a request for an action on the Shop controller is translated to a request for the Home controller.
routes.MapRoute("ShopSchema2", "Shop/OldAction",new { controller = "Home", action = "Index" });
——A request for an OldAction action on the Shop controller is translated to a request for the Index action on Home controller.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = "DefaultId" });
——The route’s URL pattern defines the typical controller and action variables, as well as a custom variable called id. This route will match any zero-to-three-segment URL. The contents of the third segment will be assigned
to the id variable, and if there is no third segment, the default value will be used.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = UrlParameter.Optional });
——
An optional URL segment is one that the user does not need to specify, but for which no default value is specified.This route will match URLs whether or not the id segment has been supplied.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional });
——If the URL contains additional segments, they are all assigned to the catchall variable ,segments captured by the catchall are presented in the form segment/segment/segment.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.Controllers" });
——In the listing, we have told the MVC Framework to look in the URLsAndRoutes.Controllers namespace before looking anywhere else. If a suitable controller cannot be found in that namespace, then the MVC Framework
will default to its regular behavior and look in all of the available namespaces.
Route myRoute=routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.Controllers" }); myRoute.DataTokens["UseNamespaceFallback"] = false;
——To disable searching for controllers in other namespaces, we must take the Route object and set the UseNamespaceFallback key in the DataTokens collection property to false.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },new { controller = "^H.*" },new[] { "URLsAndRoutes.Controllers" });
——Using a constraint with a regular expression that matches URLs only where the value of the controller variable begins with the letter H.
Access any of the segment variables:
public ViewResult CustomVariable() { ViewBag.CustomVariable = RouteData.Values["id"]; return View(); }
Using Custom Variables as Action Method Parameters:
If we define parameters to our action method with names that match the URL pattern variables, the MVC Framework will pass the values obtained from the URL as parameters to the action method.