Technical Principle Analysis [4: My record framework Routing System]

Source: Internet
Author: User

Source code location: wojilu. Web. Mvc. Processors: RouteProcessor. cs

: Wojilu. Web. Mvc. Routes: RouteTool. cs
The client sends a request to the server in the form of a url link. The url tells the server what information should be returned.
The purpose of the route system is to parse the url into specific data, such as what the controller in the url is, what the action is, and what the id is, the page number.


If you still remember the previous article, after the wojilu system obtains the page request through IhttpHandler, it first executes RouteProcess. Correct, the first step is to route the request URL.

Config->

Wojilu. Web. Mvc. CoreHandler. ProcessRequest->

Wojilu. Web. Mvc. CoreHandler. ProcessRequest: ProcessContext. Begin->

RouteProcess


 

Let's take a look at what is done in the main method of RouteProcess:
 

1 public override void Process (ProcessContext context ){
2
3 MvcEventPublisher. Instance. BeginParseRoute (context. ctx );
4 if (context. ctx. utils. isSkipCurrentProcessor () return;
5
6 Route r = RouteTool. Recognize (context. ctx );
7 context. ctx. utils. setRoute (r );
8
9 IsSiteClosed (context. ctx );
10}
First, broadcast to the system through EventPublisher. Now we need to start a route parsing process. If there are other listeners, we can perform some preprocessing. Next, let's see if this Process can be Skip removed. In wojilu, you can set whether the next Process can be Skip (skipped) at the end of the current Process ). Of course, the reason here is that the first Process won't be Skip.


There is a RouteTool in wojilu mvc, which is specially used to parse URLs. The parsed route information is placed in ProcessContext and passed to the next Process.

ProcessContext is a huge content container containing all the information of each request. After each Process, the content may be changed or appended.
Finally, determine whether the current website is closed. Wojilu can use the background to control whether the website is made public. If the website is currently closed, you need to check whether the webpage is being accessed at the website background. If the webpage is logged on at the website background, perform subsequent operations. If the website is closed, if it is not the website background login page, the preset reason for closing is output to the user request page, and the request processing is complete.
 

1 private void IsSiteClosed (MvcContext ctx ){
2
3 Boolean isAdmin = isSiteAdmin (ctx. route );
4
5 if (! Config. Instance. Site. IsClose | isAdmin! = False) return;
6 ctx. web. ResponseWrite (config. Instance. Site. CloseReason );
7 ctx. web. ResponseEnd ();
8}
9
10 private Boolean isSiteAdmin (Route route ){
11 String ns = route. ns;
12 if (strUtil. IsNullOrEmpty (ns) return false;
13 if (ns. StartsWith ("Admin.") | ns. Equals ("Admin") return true;
14 return false;
15}
 

Route r = RouteTool. Recognize (context. ctx );

The Recognize method is used to obtain the result, that is, a Route object. The values of each attribute are:
Ctx. route. id current ID (integer)
Ctx. route. controller current controller (string)
Ctx. route. action Current Controller method (string)
Ctx. route. owner accessed object (string)
Ctx. route. ownerType type of the object to be accessed (such as site/group/user) (string)
Ctx. route. appId the appId of the current application (sometimes it can be 0) (integer)
Ctx. route. page Current page (displayed when turning pages) (integer)
Ctx. route. query string (QueryString) in the current url)

 

After wojilu mvc parses and obtains these values, it passes them to MvcContext. Then, the framework creates a controller object based on the controller and action strings. These processes are automatically processed by the framework. Therefore, developers generally do not need to use string values such as ctx. route. controller.

The following is an example. For example, after the url Article/2/Edit. aspx is parsed, it actually corresponds to the Edit (int id) method of ArticleController;
The url Article/2. aspx corresponds to the Show (int id) method of ArticleController after it is parsed. The default Show method is not displayed in the url.

Basically, developers don't have to worry about link generation and route parsing. You need to understand the following content only when you need to customize route resolution rules.

I. conventional routing

The routing rules are defined by the configuration file/framework/config/route. config:


Let's look at the third line. This rule divides the url into two parts: controller and id. If a url is composed of two parts, and the second part meets the following requirements (requirements) is an integer, so the first part of the url is the controller, and the second part is the id, such as Article/3. the aspx Controller is ArticleController (the Controller suffix is automatically added), and the ID is 3.

The following lines of routing rules are parsed in sequence and so on.

So what does the first route (the first line) mean? It indicates that if the requested url is default. aspx, MainController is used by default, but no default action is specified. If there is no id, the default action is Index; if there is id, the default action is Show.

Ii. Custom Routing

Some web site is relatively long, such as www.wojilu.com Forum web site is: http://www.wojilu.com/Forum1/Forum/Index in the domain name after the path is/Forum1/Forum/Index, not easy to remember, at this time, custom routing is useful. Add a line in route. config:
Bbs; default: {ownertype = site, owner = site}

You can compare it with the first line in the original route. config. It means that bbs. aspx will be parsed to the default controller action of the site object, but it is strange that the default controller is not defined? This is an additional feature added to the "My recorded website Integrated System" based on routing. The default controller is determined based on the menu settings in the database. For more information about this part, see "My recorded website Integrated System ".

We can even direct the home page to a specific user. For example, if the friendly url of a user name is zhangsan, modify the first line to the following:
Default; default: {owner = zhangsan, ownertype = user}

Then, when you access the home page of the website, you will see the home page of zhangsna. By the way, this means that you can use the "My recorded website comprehensive system" multi-user system as an independent blog. Compared with those single-user blog systems, it is much more convenient and powerful. This is like using windows 2003 as windows xp and wondows 2008 as windows 7. In addition, the advantage of using this method is that once your independent blog is well known, you can simply modify the route, convert it to a multi-user system that supports "Forum/SNS.
Iii. query parameters
Let's take a look at the traditional method. In traditional web development, parameters are transmitted to another page through queryString, that is, a question mark and several parameter lists are attached to the URL. For example:
/Product. aspx? Factory = Lenovo & category = pc
Then, the values of factory and category are obtained through Request. QueryString ("factory") and Request. QueryString ("category") on the server.
This method is still supported in the wojilu framework. However, for Request. QueryString

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.