ASP. net mvc routing (2)

Source: Internet
Author: User

ASP. net mvc routing (2)

 

Preface

In the previous article, we mentioned some information about the Route and RouteCollection objects and their structures. Let's take a look at the handling process and explain the remaining questions that have not been resolved.

URL rule generation

The name of a Url rule is quite scary. In fact, We parse a pattern based on our custom Url and wait for the requested Url to come, match with the pattern we define (for example ). This is subsequent content.

In the previous article, we mentioned that the definition of URL rules is in the Route object. The following describes how to convert a Route object into a URL rule based on a user-registered URL.

We will use the previous example for demonstration. As mentioned in the previous article, the "{control}/{action}/{id}" parameter in the MapRoute method indicates the Url attribute of the Route object to be assigned a value, it is also said that the Set accesser will perform some other operations when assigning the Url attribute of the Route object. Example code 1-1:

Code 1-1

1 public class Route 2 {3 private string _ url; 4 private ParsedRoute _ parsedRoute; 5 public string Url 6 {7 get {return _ url ;} 8 set 9 {10 _ url = value; 11 // parse Value value 12 this. _ parsedRoute = RouteParser. parse (value); 13} 14} 15 ...... 16}

Here, only part of the Route object is displayed for demonstration. In code 1-1, we can see that the RouteParser type is responsible for converting the received Url value to a ParsedRoute type object.

This ParsedRoute type object is an internal private type of the Route type, and the converted Url mode information is stored in this type of object instance. See Figure 1:

Figure 1

As shown in figure 1, when a Url Value of "{controller}/{action}" is assigned to the Url attribute of the Route object, the internal type of Parse () of RouteParser is () the static method is used to parse the Url and convert it to the internal ParsedRoute type. The constructor of the ParsedRoute type is composed of a set of PathSegment types. The MVC Framework does not use the PathSegment type, but uses the derived class of the PathSegment type (2), and these derived classes correspond to different Url fragments respectively, for example, "{controller}/{action}" is represented as two types respectively (as described below ).

Figure 2

The object model shown in Figure 2 follows the model structure shown in figure 2. In this case, the Url mode value stored in ParsedRoute is shown in figure 3,

Figure 3

3. In the preceding example, "controller" is generated as the ContentPathSegment type, and "/" is generated as the SeparatorPathSegment type. The details of these types are not described in detail here. The purpose is to roughly describe the internal implementation of the Object Model. The purpose is to let everyone know what kind of implementation method there is such a process to generate URL rules, let everyone know that the Route object itself is subject to URL rules. When server requests arrive, many Route objects are matched according to their own URL rules, which will be mentioned later.

If you are interested in clearly understanding the internal implementation details, you can use the decompilation tool to view the source code. This part of the Url rule generation process is described here.

I hope you can remember the general process and the location of these objects in the system. In the next article, we will explain how RouteData is generated based on the requested Url.

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.