ASP. NET route resolution

Source: Internet
Author: User

ASP. NET route resolution
During this period, I learned about the design idea of the MVC model from the underlying layer by reading ASP. NET MVC5 framework secrets by Artech. The following is a summary of reading. In traditional Web Forms applications, URLs point to specific physical files, while ASP. net mvc applications generally point to an Action method in a Controller. The ing between the URL and the Target Controller/Action is achieved through "routing. Description of several core classes in the routing system: RouteBase is an abstract base class. Public abstract class RouteBase {//. NET Framwork4.5 does not have this attribute (whether to route physical files) public bool RouteExistingFiles {get; set ;}// obtain route data public abstract RouteData GetRouteData (HttpContextBase httpContext ); // route parsing generates a complete path public abstract VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values);} RouteData encapsulates route information. RouteData returns a RouteHandler object through its RouteHandler attribute. RouteHandler implements GetHttpHandler (RequestContext requestContext) in IRouteHandler and returns a HttpHandler object to take over the http request. Public class RouteData {public RouteData (); public RouteData (RouteBase route, IRouteHandler routeHandler); public string GetRequiredString (string valueName); public RouteBase Route {get; set ;} public IRouteHandler RouteHandler {get; set;} public RouteValueDictionary DataTokens {get;} public RouteValueDictionary Values {get;} VirtualPathData indicates information about routes and virtual paths. Perform RouteBase's GetVirtualPath () to perform route matching, replace the routing variable with the placeholder in the routing template, and generate a virtual path. This class is the encapsulation of virtual paths and Route. Public class VirtualPathData {public VirtualPathData (RouteBase route, string virtualPath); public RouteValueDictionary DataTokens {get;} public RouteBase Route {get; set;} public string VirtualPath {get; set ;}} route provides the attributes and methods used to define routes and obtain Route-related information. Route resolution is completed by a specific Route object in the Route table. Public class Route: RouteBase {public Route (string url, IRouteHandler routeHandler); public Route (string url, RouteValueDictionary defaults, IRouteHandler routeHandler); public Route (string url, RouteValueDictionary defa, routeValueDictionary constraints, IRouteHandler routeHandler); public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionar Y dataTokens, IRouteHandler routeHandler); // constraint (available regular) public RouteValueDictionary Constraints {get; set;} // stores additional variables and does not participate in route parsing public RouteValueDictionary DataTokens {get; set;} public RouteValueDictionary Defaults {get; set;} public IRouteHandler RouteHandler {get; set;} public string Url {get; set;} public override RouteData GetRouteData (HttpContextBase httpContext ); public override Virtu AlPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values);} RouteTable stores the URL route of the application. The static read-only attribute Routes accesses the global route table. Public class RouteTable {public static RouteCollection Routes {get ;}} RouteCollection is a set of Route operations. When you call the GetRouteData and GetVirtualPath of RouteCollection, all the Route in the set is traversed. Two common methods. MapPageRoute: register a route. Ignore: Ignore the corresponding URL format. Summarize the relationships between classes: the Route object represents an actual routing rule. Route Parsing is performed when two Route matching methods of the Route object are called. The returned RouteData or VirtualPathData is the encapsulation of Route. RouteTable stores the global routing information of Web applications, that is, multiple Route objects. The default route in route registration MVC4 registers public static void RegisterRoutes (RouteCollection routes) {routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. mapRoute (name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index ", id = UrlParameter. optional});} The registration method with constraints in the book public static void RegisterRoutes (RouteCollection routes) {// default value var defaults = new RouteValueDictiona Ry {"areacode", "010" },{ "days", 2 }}; // regular constraint var constaints = new RouteValueDictionary {"areacode ", @ "0 \ d {2, 3}" },{ "days", @ "[1-3] {1 }"}}; // description var dataTokens = new RouteValueDictionary {"defaultCitr \ y", "BeiJing" },{ "defaultDays", 2}; routes. mapPageRoute ("default", "{areacode}/{} days ","~ /Weather. aspx ", false, defaults, constaints, dataTokens);} The regular expression constraint is a simple method. We can also implement it through custom constraints. Implement the Match method of IRouteConstraint. Suppose we want to restrict Internet Explorer access through IRouteConstraint. Public class IERouteConstraint: IRouteConstraint {public bool Match (HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {return! (HttpContext. request. userAgent. contains ("MSIE") ;}// based on the original constraints, modify var constaints = new RouteValueDictionary {"areacode", @ "0 \ d {2, 3 }"}, {"days", @ "[1-3] {1}"}, new IERouteConstraint ()}; Note: For details about custom route constraints, go: http://www.cnblogs.com/xfrog/archive/2010/12/19/1910428.html . This example is also selected from this blog. The IHttpModule is derived from the HTTP request to the route parsing URLRoutingModule. Use it to register the PostResolveRequestCache event of HttpApplication. When an HttpApplication object triggers this event, the URLRoutingModule obtains the RouteCollection object of the global route table through the static read-only attribute Routea of RouteTable, and then creates an HttpContextWrapper object (derived from HttpContextBase) according to the current context ), call the GetRouteData method of the RouteCollection object as a parameter. If the route match is successful, a specific RouteData object is returned. The URLRoutingModule transmits HttpContextWrapper to RouteHandler in the RouteData object. Call its GetHttpHandler method to obtain a specific HttpHandler. URLRoutingModule finally calls the RemapHandler method of the HttpContextWrapper object to map the obtained HttpHandler. After the resolution is complete, the Handler will take over the current HTTP request.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.