Some content about ASP. NET Routing

Source: Internet
Author: User

In previous articles, some friends will ask me about ASP. NET Routing. This component becomes more and more important. ASP. net mvc and ASP. NET Dynamic Data all use ASP. NET Routing. In fact, ASP. NET 4.0 also supports ASP. NET WebForms. Unfortunately, there are few documents and descriptions about ASP. NET Routing. Therefore, sometimes some friends may not be able to understand some of my expansion design ideas. Now I want to explain in detail the most common problems related to ASP. NET Routing.

What is the role of ASP. NET Routing?
ASP. NET Routing has two functions:

Capture data from the request. Generate a virtual path from the data.

It can be seen that ASP. NET Routing provides two-way functions. It is worth noting that these two operations are strictly asymmetrical. Because 1st points capture data from the "request", and the data source is not necessarily a URL, it may be Server Variables, it may be a Header; correspondingly, the virtual path generated from the data, a URL and a string must be generated.

ASP. NET Routing, we will use ASP. the Route class in the NET Routing component. It obtains data from the Virtual Path and provides advanced functions such as default values and constraints. However, the ASP. NET Routing component, or its "engine", uses an abstract type "RouteBase ". Route is just an implementation of RouteBase.

In my previous articles, I have mentioned some other extensions, such as FormatRoute or DomainRoute.

Why does Route not use regular expressions?
The Route class in ASP. NET Routing captures data from the virtual path of the request based on the specified "path template. This "template" is similar to this form:

{Controller}/{action}/{id}. Based on the virtual path of the current request, the controller, action, and id values can be captured. But some friends said, why not use a regular expression to capture data? For example:

(? <Controller> \ w + )/(? <Action> \ w + )/(? <Id> \ d +) use the regular expression of the name capture group to virtualize the path. You can also obtain the controller, action, and id values. In addition, the advantage of using a regular expression is that the characters in the path can be strictly restricted. For example, the above regular expression limits the controller and action to word characters and the id to numbers, in this way, matching cannot be obtained if this form of path is not met. But where is the problem?

The problem here is: ASP. NET Routing works in two directions. If a regular expression is used to capture data, it is easy to generate a string from the regular expression, due to the wide range of regular expressions, it is often impossible to reverse obtain a string. Therefore, if you want to restrict a value, you can only provide one constraint for the Route object:

New {controller = "\ w +", action = "\ w +", id = "\ d +, you can implement a subset of Regular Expression matching rules to make it feasible to generate strings.

ASP. NET Routing Workflow
ASP. NET Routing can process requests and generate virtual paths. The process for processing requests has been discussed in detail in the previous article. Here we will talk about how ASP. NET Routing generates virtual paths.

ASP. NET Routing still uses the RouteCollection method when generating a virtual path. Naturally, we are like RouteTable. the Routing rules registered in the Routes attribute are also from RouteTable. obtain the virtual path from the Routes attribute. The RouteCollection type has a GetVirtualPath method, which returns a VirtualPathData object, which contains a VirtualPath attribute, which is the final virtual path.

The GetVirtualPath method has two reloads. The first one is:

Public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values) The RequestContext object is the context of the current request, while RouteValueDictionary contains data used to construct the virtual path. Similar to the request processing method, the GetVirtualPath method of the RouteCollection type also calls the GetVirtualPath method of each RouteBase object in turn and returns the first non-null result (in fact, it will be processed again, and see the following ). If none of the RouteBase objects match the currently provided data, the GetVirtualPath of RouteCollection returns null.

Another overload of the GetVirtualPath method will provide a name parameter:

The public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values) parameter specifies a Routing rule, that is, a specific RouteBase object. If the RouteBase object returns null, The GetVirtualPath method returns null directly. The advantage of specifying a name is that the Code is highly readable (developers can directly find the corresponding Routing policy), the performance has certain advantages (no need to traverse each Routing rule), and there is no conflict between the Routing rules.

Does ASP. NET Routing Support domain names?
Not supported. It is not supported in design.

This can be seen from the implementation of the GetVirtualPath method of RouteCollection. This method does not return immediately after traversing each RouteBase object and obtaining the first result not null, you must also use the GetUrlWithApplicationPath method for processing:

Private static string GetUrlWithApplicationPath (RequestContext requestContext, string url)
{
String str = requestContext. HttpContext. Request. ApplicationPath ?? String. Empty;
If (! Str. EndsWith ("/", StringComparison. OrdinalIgnoreCase ))
{
Str = str + "/";
}

Return requestContext. HttpContext. Response. ApplyAppPathModifier (str + url );
} The url parameter indicates a "virtual path". The function of the GetUrlWithApplicationPath method is to add an "Application directory" to it ". For example, a website may not be deployed under "/", but in "/MvcApp. In this way, although RouteBase's GetVirtualPath returns the virtual path "Home/Index/5", the GetVirtualPath method of RouteCollection is processed using GetUrlWithApplicationPath, the final returned path is a string such as/MvcApp/Home/Index/5.

Therefore, ASP. NET Routing does not support the concept of domain names. Although we can extend RouteBase objects, we cannot extend the GetVirtualPath method of RouteCollection. In other words, even if our DomainRoute can return. In other cases, the behavior of the GetVirtualPath method is the same as that of the original GetVirtualPath method.

If you want to use DomainRoute, use GetVirtualPathEx instead of the original GetVirtualPath method.

Okay, I admit that this title has eight shares.

From: http://www.cnblogs.com/JeffreyZhao/

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.