ASP. NET MVC Calf tour 3:routing--URL Routing

Source: Internet
Author: User

URL Routing (Routing) has two main uses in ASP. NET MVC, one purpose is to match the HTTP request from the browser, and the other is to respond to the appropriate URL to the browser .

3.1 Matching HTTP requests coming through the browser

First, let's take a look at the first use, which is to match the HTTP request coming from the browser.

When a client makes a request to an ASP. Routing can find the appropriate HttpHandler to process the Web page, the approximate process is:

If HttpHandler is handled by Mvchandler, then it will enter the execution life cycle of ASP, and the appropriate controller and action will be found to process it and feed back to the client.

Example: First run the default MVC project you created last, then click on the "about" link in the upper right corner:

At this point, the URL of the browser's URL column will become the following, note the URL path section:

http://localhost:28039/home/about

This click-Link action, in fact, will allow the browser to convert the URL into an HTTP request packet, and the browser issued an HTTP request to the server, the first time IIS received this HTTP packet. The routing module is then forwarded to the URL to determine which Httphander processing this HTTP request will be handed over to. In this case, of course, it is transferred to Mvchander processing, and then into the adp.net MVC run life cycle.

3.2 Respond to the appropriate URL to the browser

Another use of URL routing is to decide what URLs ASP. NET MVC should output and return it to the browser, as a general rule, you need to refer to the definition of URL routing when you want to jump to an address or display a hyperlink in a view, as this will dynamically determine what URL the ASP output should be.

3.3 Default URL routing attribute analysis

The default URL routing rule is defined in the App_start\routeconfig.cs document:

The markings in the figure are as follows:

① all the ASP. NET Web application entry points are in the HttpApplication Application_Start () event, Routetable.routes is a public static object, The rule set used to hold all URL routes (routecollection) in Global.asax Application_Start () event plus routeconfig.registerroutes ( Routetable.routes) line, pass the routetable.routes variable into the routeconfig.registerroutes static method, code:

② in the default RegisterRoutes method Ignoreroute, is used to define not through the URL route processing of URLs , if the comparison is successful, will not continue to compare, so will not go into the ASP. NET MVC run life cycle.

③{resource} represents a routevalue routing variable that represents a "location" that can place a variable that cannot be used, *.axd (document about WebForm)

④{*pathinfo} is also a routevalue route variable, * represents fetch all meaning, this routing path will be the complete path information in addition to ③ the URL obtained.

⑤maproute is the method used to define the route

The ⑥name parameter defines the route name

The ⑦url named parameter defines the URL style with the Rotevalue parameter name for each path paragraph. URL: "{controller}/{action}/{id}", meaning that if we enter the URL path is/home/about/123, then the controller's route value is home,action the route value is about, E and the ID's route value is 123.

The ⑧defaults named parameter defines the default value for the route parameter, and when the URL route comparison does not find an HTTP request, it first attempts to bring in the default value defined here, and then compares whether there is a compliant controller and action to run.

3.4 Conditional constraints for URL routing plus route values

Maproute is the most common extension method for defining URL routing rules with many overloads, constraints This named parameter adds some conditional constraints to the route value:

This means that the ID route value is fully numeric to compare success.

ASP. NET MVC Calf tour 3:routing--URL Routing

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.