6th: deep understanding of ROUTES

Source: Internet
Author: User

2010.9.6 su Peng

Content

-Use routing to generate a URL

-URL and Action matching secrets

-Use routing in WebForm

 

Prerequisites

-Install Visual Studio 2010 Express

-Understand ASP. Net

-Understand the basic concepts of the Design Mode

 

How to generate a URL for Routes

-Bidirectional URL matching policy

 

Strictly speaking, the ASP. Net MVC Architecture has more than three layers. In fact, it has four layers. The fourth layer is the hub layer, that is, the Roues object. It is an important part of the MVC Architecture to survive.

 

Url generator Overview

-1. consult each route table

-2. Matching returned results

 

In essence, the Routing system only abstracts two classes: RouteCollection class and RouteBase class.

The Url constructor must call the GetVirtualPath method of the RouteCollection class at the beginning. This method is used to return the virtual path. It is a method that actually extracts the path from the corresponding dictionary list. The returned results of this method will be passed to RequestContext, and the information is specified as the user's route information. The information will be placed in the dictionary. Then, we will use the parameter matching in the dictionary to generate a Url.

Working Method:

First, ask all route tables and ask them: Does the Url you generated contain the following parameters? This method is similar to splitting a URL when matching a URL. When a route says I can, and I can return it, it will return a VirtualPath instance, which contains a URL attribute. If none of them match, a Null value is returned. Then, Routing constructs the next route list in sequence based on the route information.

 

Detailed working method of URL Generator

-1. The user calls RouteCollection. GetVirtualPath

-2. Matching Parameters

-3. Match the default parameter

 

Use named route

By default, RouteCollection. GetVirtualPath does not know the route to do this. If the named route is specified for query, it is clear that the row or failure occurs.

 

Matching of unspecified variables

Unspecified variables, that is, in some cases, the URL submitted by the user contains some other data, which is not the specified data. Any such data is called the matching of environment variables. In these matches, GetVirtualPath cannot directly obtain the object address. Therefore, you must use another method.

For example, when a page is paginated, the parameters input by the URL may contain the number of pages in the current list. However, for buttons on the "Previous Page" or "next page, it does not know which page it points to. Therefore, to reuse controls, Route needs to add new elements.

The code above constructs a virtual path pointing to the real number of pages equal to the next Page. It creates a new RouteValueDictionary class, which records the information of each URL, the names of all parameter values, and the values of parameters.

 

Append Parameters

-If the value of the generated url parameter exceeds the value of the matched item, a new parameter matching record will be generated in VD (RouteValueDictionary). This record will be appended to the url as a parameter and used
? Connection

-What is the url Connection method? Parameter Name = parameter value

 

Complete example

How does Routes map URLs to actions?

-Request Routing Pipeline

 

Request pipeline Overview

1. The UrlRoutingModule view uses the registered route in RouteTable to process the current request.

2. If the matching succeeds, the IRouteHandler object is generated from the route object.

3. the Routing module calls the GetHandler method in IRouteHandler. This method returns an IHttpHandler. (IHttpHandler is the path of a standard aspx page, which is actually the same as ashx)

4. The ProcessRequest object calls the HttpHandler object response.

5. In the MVC Architecture, the IRouteHandler object is a MvcRouteHandler object by default, and the returned object is a MvcHandler object.

 

Route matching rules

-Routedata

In essence, the IRouteHandler object obtains request data and route information. In a macro sense, when a request comes over, the UrlRoutingModule cyclically accesses the Routes object of the associated RouteTable in each RouteCollection object. Then, it asks, can you process this request? If the other party says yes, give it to you. The key is whether a route can process the request by calling the GetRouteData method. Therefore, every Routes object must call GetRouteData. If the return value is null, the route does not match. If a RouteData object is returned, the router can be matched. In essence, there is no direct dialogue between a route and a route. If a request is sent by a third party, the request is made. You need to use RouteData to determine the data passing keywords.

 

RouteData

RouteData is actually an instance of RouteValueDictionary, which exists in every MvcRouteHandler object.

 

Constraints

-Add User-defined constraints to routes

These five parameters can be reloaded by us. For example, if we want to restrict the request to Get but not Post, we will reload HttpContextBase to determine its method.

The fourth parameter of MapRoute is the constraint object.

 

Route Extension

-Match the route parameter with any number of parameters.

 

Edit Routes

 

Use Cache monitoring code

Follow Routes files in factory Mode

 

Use Routing in WebForm

-Use Routing in ASP. Net 4

-Use Routing in ASP. Net 3.5

 

ASP. Net WebForm 4

Use Routing in ASP. Net 3.5

-In ASP. net 3.5 is not as convenient as 4. Of course, we still have a way to implement the IRouteHandler interface, specifically, this operation is implemented by defining a new class in WebForm.

 

Summary

ASP is introduced in both lectures. net MVC's pivot Routing technology mainly introduces three aspects: Routing Forward matching, Url-based Routes splitting, and corresponding Controller and Action. Let's talk about the theory. Next we will talk about how to generate a URL, mainly using the GetVirtualPath method of RouteCollection. All environments are based on the IIS7 standard environment.

2010.9.27

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.