Notes on learning ASP. NET MVC5 framework-ASP. net mvc routing (2)
2.2.2 route Registration
ASP. net mvc registers a route by calling the extension method MapRoute of the RouteCollection object representing the global route table. Let's take a simple example. We still use the previous routing template for obtaining weather information to see what kind of RouteData object will be returned for matching requests registered using this method.
We create an empty ASP. NET Web program, and manually add "System. Web. Mvc. dll" and "System. Web. WebPages. Razor. dll" references. Add the next page (Default. aspx) as shown in the following figure, and display the RouteData attributes in the internal join mode according to the previous practice. The displayed RouteData is obtained by calling the custom GetRouteData method, rather than the RouteData object returned by the RouteData attribute on the current page.
The GetRouteData method is defined in the background code of the current page. As shown in the following code snippet, an HttpContext object is created based on the manually created HttpRequest (the request URL is "http: // localhost/0512/3") and HttpResponse object, create an HttpContextWrapper object. Next, we use the static RouteTable attribute Routes to obtain the RouteCollection object representing the global route table, and call the HttpContextWrapper object as a parameter to call its GetRouteData method. This method is used to simulate the route resolution of the registered route table for requests whose relative address is "/0512/3.
ublic partial class Default : System.Web.UI.Page { private RouteData routeData; public RouteData GetRouteData() { if (null != routeData) { return routeData; } HttpRequest request = new HttpRequest("default.aspx", "http://localhost/0512/3", null); HttpResponse response = new HttpResponse(new StringWriter()); HttpContext context = new HttpContext(request, response); HttpContextBase contextWrapper = new HttpContextWrapper(context); return routeData = RouteTable.Routes.GetRouteData(contextWrapper); } }
The specific route registration is still defined in the added Global. asax file. As shown in the following code, RouteCollection Objects representing the global route table are obtained using Routes, the static attribute of RouteTable, then, call its MapRoute method to register a Route object using "{areacode}/{days}" as the routing template, and specify the default values, constraints, and namespace lists of variables. Because the successfully matched route object must have a routing variable named "controller", we directly set the default value of controller to "home ".
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { object defaults = new { areacode = "010", days = 2, defaultCity = "BeiJing", defaultDays = 2, controller = "Home" }; object constraints = new { areacode = @"0\d{2,3}", days = @"[1-3]" }; string[] namespaces = new string[] { "Artech.Web.Mvc", "Artech.Web.Mvc.Html" }; RouteTable.Routes.MapRoute("default", "{areacode}/{days}",defaults, constraints, namespaces); } }