Asp.net route Learning 1
Starting from today, I plan to take a good look at the book ASP. NET MVC5 framework secrets, hoping to improve the level. Of course, I still need to write my study notes. Asp.net routing: IIS provides us with a url rewriting mechanism, but this solves the separation between URLs and physical addresses at the iis level, its implementation relies on a local code module registered to the IIS pipeline, while asp.net routes are part of the asp.net mechanism and are written through managed code. So what is the asp.net mechanism? First, understand some common classes and objects. 1. RouteBase class. The core of the routing system is the Route object. Each Route registration (in different url modes) corresponds to a Route object. These Route objects are registered in the same Web application to form a Route table. Routes represents the static property of the Route object stored in the RouteTable class. This property returns a RouteCollection object. Here, Route refers to an object of a certain type inherited from the abstract class RouteBase.
Public abstract class RouteBase {// route resolution in the GetRouteData method is implemented to obtain route data public abstract RouteData GetRouteData (HttpContextBase httpContext ); // The GetVirtualPath method generates a complete virtual path through route resolution public abstract VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values); // It indicates whether to route existing physical files, the default value is true. That is, you cannot access an existing physical file through a url. You can only use the routing registry. Public bool RouteExistingFiles {get; set ;}}
GetRouteData returns a RouteData object used to encapsulate route data. RouteData has a RouteBase attribute Route, which returns the Route object that generates the RouteData. There are also DataTokens and Values attributes, both of which return the RouteValueDictionary object. RouteValueDictionary is a dictionary that implements the IDictionary <string, objects> interface and is used to save routing variables. The difference between Values and DataTokens is that Values is obtained by parsing the request url, and DataTokens is a custom variable directly attached to the route object. The RouteData class also has a very important attribute: RouteHandler, which plays an important role in the entire routing system, because the HttpHandler object used to process the request is provided by GetVirtualPath, A VirtualPathData object is returned. When this method is executed, if the variables in the defined routing template match the list of specified variables, it will use the specified routing variable value to replace the placeholders in the template, in this way, the virtual path is obtained. The generated virtual path and Route object are eventually encapsulated into a VirtualPathData object as the return value. This method also has a parameter of the RequestContext type.
Public class RequestContext {// initialize a new instance of the System. Web. Routing. RequestContext class. Public RequestContext (); // httpContext: an object that contains information about HTTP requests. // routeData: an object, this object contains information about the routes that match the current request. public RequestContext (HttpContextBase httpContext, RouteData routeData); // Abstract: gets information about HTTP requests. // Return result: an object that contains information about HTTP requests. Public virtual HttpContextBase HttpContext {get; set ;}// Abstract: gets information about the requested route. // Return result: an object that contains information about the requested route. Public virtual RouteData {get; set ;}
2. Route class RouteBase is an abstract class. In the Application Programming Interface of ASP. NET routing system, the Route type is its unique direct successor. This class has a Url attribute, which indicates the routing template bound to the route object. When a request comes over, it matches the request Url Based on the url attribute in the Route object. This is the Route resolution. In addition to the core attribute Url, the Route type also has some other attributes. Constraints sets some Constraints for the variables in the template. The attribute type is RouteValueDictionary, and its key and Value are the variable names and the regular expressions used as Constraints respectively. Defaults also returns a RouteValueDictionary object, it saves the default value defined for the routing variable. Route-type DataTokens is used to store some additional routing variables, which are not involved in Route resolution for requests, however, the Route variables contained in the DataTokens in the object obtained by calling the GetRouteData and GetVirtualPath methods of the Route type are derived from this.