ASP. net mvc mavericks tour 3: Routing -- URL Routing, mvcrouting

Source: Internet
Author: User

ASP. net mvc mavericks tour 3: Routing -- URL Routing, mvcrouting

Routing)ASP. net mvc has two main purposes:Matches HTTP requests sent from the browser,Another purpose isRespond to the appropriate URL to the browser.

3.1 match HTTP requests sent through the browser

First, let's take a look at the first purpose, that is, matching the HTTP request sent through the browser.

When a client sends a request to an ASP. NET Website, it can use Routing to find a suitable HttpHandler to process the webpage. The general process is as follows:

If HttpHandler is handled by MvcHandler, then ASP is entered. net mvc execution lifecycle, and find the appropriate Controller and Action to process it, and feedback the information to the client.

Example: run the default MVC project created last time, and click the "about" link in the upper right corner ,:

In this case, the URL in the URL column of the browser changes to the following. Note the URL path section:

Http: // localhost: 28039/Home/About

This clicking action actually allows the browser to convert the URL into an http request packet, and the browser sends an http request to the server, at this time, iis will first receive the http packet and then forward it to the URL routing module to decide which HttpHander to send the http request. In this example, it is of course transferred to MvcHander for processing, and then enters the running life cycle of ADP. net mvc.

3.2 respond to the appropriate URL to the browser

Another purpose of URL routing is to determine ASP. net mvc should output what kind of URL and return it to the browser. Generally, when you jump to the URL or display a hyperlink in the View, you need to refer to the URL routing definition, in this way, ASP can be dynamically determined. net mvc output URL.

3.3 default URL routing Attribute Analysis

The default URL routing rules are defined in the App_Start \ RouteConfig. cs document ,:

The labels in the figure are described as follows:

① All ASP. NET Web program entry points are in the Application_Start () event of HttpApplication, and RouteTable. Routes is publicStatic object,The rule set (RouteCollection) used to save routes for all URLs is located at Global. add RouteConfig to the Application_Start () event of asax. registerRoutes (RouteTable. routes) line, set RouteTable. the Routes variable is passed into RouteConfig. registerRoutes static method, code:

② In the default RegisterRoutes methodIgnoreRouteIs used to defineDo not process URLs through URL RoutingIf the comparison is successful, the comparison will not continue, so it will not enter the ASP. net mvc runtime lifecycle.

③ {Resource} represents a RouteValue routing variable, which represents a "location" and can be placed with an unused variable *. axd (about WebForm Documentation)

④ {* PathInfo} is also a RouteValue routing variable. * Indicates capturing all the meaning. This routing path will be the complete path information except the URL obtained by ③.

⑤ MapRoute is a method used to define a route

⑥ The name parameter defines the route name

7. The url-named parameter defines the RoteValue Parameter Name of the URL style and each path section. Url: "{controller}/{action}/{id}", which means that if the url we entered is/Home/About/123, then, the controller's route value is Home, the action's route value is About, and e, and the id's route value is 123.

⑧ Defaults: the name of the parameter defines the default value of the route parameter. When the HTTP request cannot be found in the URL routing comparison, the default value defined here is first attempted, then compare whether there are qualified controllers and actions that can be run.

3.4 add routing value constraints to URL Routing

MapRoute is the most common extension method for defining URL routing rules. There are many overload methods,ConstraintsThis named parameter adds some constraints to the route value ,:

This means that the comparison is successful only when the id route value is all numbers.

 

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.