URL routing (III) of ASP. net mvc Framework)

Source: Internet
Author: User

I. Basic concepts:

1. the routing component is system. Web. Routing, which is released together with. Net 3.5 SP1. Therefore, this component is not open to source code.
2. You can also use this component in ASP. NET webform;
2. ASP. net mvc extends and defines rules. With the new "maproute" auxiliary method, it provides extremely simple syntaxes, but can do the same thing.

2. Role of routing:

As you can see, routing is used
1. define how to select the Controller class,
2. define which action method to call,
3. The variable value is automatically parsed from the URL/querystring and passed as a parameter to the method.
Next, the Controller executes the corresponding method.

3. Configure routing:

1. configuration.Generally, we add routes to the application_start event in the global. asax file to configure the routing rule,
Code in the global. asax. CS file:
Public static void registerroutes (routecollection routes)
{
// Ignore the route of the. axd file, that is, access the. axd file directly like webform.
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");

Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional} // The default value of the parameter. ID is an optional parameter.
// When the URL does not specify controller = home, Action = index, id = ""
);
// You can use routes. maproute () to add multiple rules,
// The sequence of rules is very important. Top-Down matching is performed based on the defined sequence of route, and the matching result is not executed downward.

}

Protected void application_start ()
{
Arearegistration. registerallareas ();
// Register the previously defined route rules when the program starts
Registerroutes (routetable. routes );
}

2. URL of the rule:
In a route, a placeholder is placed in braces to define ({And}). information not included in the route definition is used as a constant value.
The return value of different URL requests under the {controller}/{action}/{ID} rule.

In addition, you can use the asterisk (*) to match parameters with uncertain numbers. This matches all the remaining parameters after the URL. For example:
Query/{queryname}/{* queryvalues} rules for URL:
Query/aspnetmvc/preview5/routing, the value of the queryvalues parameter is preview5/routing.

3. parameter description:
The maproute () auxiliary method is overloaded and can accept 2, 3, or 4 parameters (path name, URL syntax, default URL parameter, and URL parameter Regular Expression constraint ).
For example:
Routes. maproute (
"Default", // The name can be specified but cannot be repeated in the context.
"{Controller}/{action}/{ID}", // URL syntax
New {controller = "home", Action = "Index", urlparameter. optional}, // default URL parameter,
New {id = @ "[/d] *"} // The ID must be a number. More complex constraints can be implemented through extension.
);

4. Differences between routing and URL rewrite:

1. url rewrite: changes the URL processed by the program.
2. url route: select and execute according to URL rules.
3. The two have different execution mechanisms.
4. In some cases, the same result can be generated.
5. url route is more like basic support
-Suitable for developing new frameworks based on ASP. NET architecture

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.