Learning ASP. NET MVC5 framework secrets note-ASP. NET routing (6), mvc5-asp.net

Source: Internet
Author: User

Learning ASP. NET MVC5 framework secrets note-ASP. NET routing (6), mvc5-asp.net
4. Route

RouteBase is an abstract class in ASP. in the Application Programming Interface of the NET routing system, the Route type is its unique direct successor. By default, the MapPageRoute method of calling RouteCollection is added to the routing table as such an object. As shown in the following code snippet, the Route type has a string type attribute Url, which represents the routing template bound to the routing object.

public class Route : RouteBase{public Route(string url,IRouteHandler routeHandler);public Route(string url,RouteValueDictionary defaults,IRouteHandler routeHandler);public Route(string url,RouteValueDictionary defaults,RouteValueDictionary constraints, IRouteHandler routeHandler);public Route(string url,RouteValueDictionary defaults,RouteValueDictionary constraints, RouteValueDictionary dataTokens,IRouteHandler routeHandler);public override RouteData GetRouteData(HttpContextBase httpContext);public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values);protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName,RouteValueDictionary values,RouteDirection routeDirection);public RouteValueDictionary Constraints { set; get; }public RouteValueDictionary Defaults { set; get; }public RouteValueDictionary DataTokens { set; get; }public IRouteHandler RouteHandler { set; get; }public string Url { set; get; }}

By default. Route resolution for requests is completed by a Route object in the Route table. Whether a Route object is selected depends on whether the request URL matches the corresponding routing template pattern. The specific matching rules are simple. We can use a simple example to describe them. Assume that we have a URL that indicates obtaining weather conditions for a region (expressed by a telephone area number) Over the next N days, as shown in the following routing template.

/Weather/{areacode}/{days}

For the preceding routing template, we use the separator "/" to split it into three basic strings, which are called "segments ". The content of a segment can be divided into "variable" and "nominal value". The former uses curly brackets ("{}"). variable name packaging (for example, "{areacode}" indicating the telephone area code and "{days}" indicating the number of days }"), the latter represents pure static text (such as "weather "). It is worth mentioning that the character comparison in the route parsing process is case insensitive, because the URL is not case sensitive.

For a specific URL, there are two basic conditions for successful matching: the number of segments contained in the URL is the same as that of the URL template, and the content of the corresponding text segment is also consistent. According to this matching rule, the following URL matches the routing template defined above.

/Weather/0512/2.

In addition to the Url that represents the core attribute of the routing template, the Route type also has some additional attributes. Attribute Constrations is a variable defined in the template that sets some constraints in the form of a regular expression. The attribute type is RouteValueDictionary, where Key and Value indicate the variable name and the regular expression used as the constraint respectively. For example, for the Url template defined above, we can specify a regular expression for the two variables so that the request address has a valid area code and the number of days to be taken as an integer. If we use this attribute to define a regular expression based on certain variables for the Route object, except for the above two prerequisites for successful matching, the corresponding segments in the verified URL must also be verified by the corresponding regular expression. In addition to using regular expressions to define constraints, you can also directly create a RouteConstraint object to represent constraints.

Defaults, another property of the Route type, also returns a RouteValueDictionary object, which saves the default value defined for the routing variable. It is worth mentioning that the routing variables with default values do not necessarily appear in the routing template. When a Route object implements Route resolution for a URL, if the URL can only match the previous part of the routing template, but the latter part is a variable and has the corresponding default value, in this case, the matching is still considered successful.

The preceding routing template is used as an example. If we set the default values of "{areacode}" and "{days}" to "010 (Beijing) "and" 2 "(the next two days) the following three URLs can match with the Route object that owns the routing template, and they can be considered as equivalent URLs.

/Weather/010/2.

/Weather/010

/Weather

We do not require the variable defined in the routing template as the content of the entire segment. In other words, a segment can contain both static text and variables. In addition, we can use the form of "{* <variable >}" to define the variable that matches the last part of the URL (which can contain multiple segments, let's just call it a wildcard variable ".

/{Filename}. {extension}/{* pathinfo}

For the preceding routing template, the first segment contains two parts: the variables "{filename}" and "{extension}" indicating the file name and extension }", and the literal used as the separator between the two. "followed by a wildcard variable {* pathinfo }. This routing template can be matched with the following URL. After matching, three variables ({filename}, {extension}, and {pathinfo}) are defined in the template }) the values are "default", "aspx", and "abc/123 ".

/Default. aspx/abc/123

The Route-type DataTokens attribute has been mentioned earlier. It is used to store some additional routing variables that are not involved in Route resolution for requests. For RouteData and VirtualPathData objects obtained by calling the GetRouteData and GetVirtualPath methods of the Route type, the Route variables contained in their DataTokens attribute are derived from this.

Related Article

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.