ASP. NET mvc2.0 URL Routing

Source: Internet
Author: User
Tags url example
This article describes the role of the routing component and provides several examples to learn ASP. NET mvc2.0 URL routing technology.

    Next, we started to talk about how our request was forwarded to the corresponding view after we registered a route in global. asax? How is the controller and action parsed? This is what the routing component does.

    Role of routing: First, it obtains the request passed by the view and parses the controller, action, and data in the URL request, next, he passes the identified data to the Controller action (Controller method ).These are two important roles of the routing component!

The following describes the use of URL routing in several examples.

Maproute () has six methods that can be reloaded. The following are five examples!

Example 1: The default routing format is provided by the system,The Default Code is as follows:

View sourceprint?
01 public static void RegisterRoutes(RouteCollection routes)
02 {
03     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
04  
05     routes.MapRoute(
06         "Default", // Route name
07         "{controller}/{action}/{id}", // URL with Parameters
08         new { controller = "Home", action = "Index", Id = urlparameter. optional} // default value of the Parameter
09     );
10   }

URL format: http: // localhost: 0000/Home/IndexThe rule is as follows:{Controller}/{action}/{ID}The simhei part is the corresponding part. The default value still exists.

The detailed match should be: http: // localhost: 0000/Custom/detials/1 Remove the host domain name and match the controller and actiong. Parse the URL through the routing component. The controller is m and the action is detials. The ID passed in the past is 1.

    This is the overload of maproute (string name, string URL, object defaults.

Example 2: Do not use the default URL routing rule

Function header:Maproute (string name, string URL );

Routes. maproute ("No Default routing rule", "{controller}/{ID}-{action }");

Applicable URL example: http: // localhost: 0000/Custom/1-detials 

It will not matchHttp: // localhost: 0000/

Example 3: URL routing rules with namespaces

     Function header:Maproute (string name, string URL, string [] namespaces); // route name, URL rule, namespace

     Routes. maproute (
             "Myurl", // route name
             "{Controller}/{ID}-{action}", // URL with Parameters
             New {controller = "home", Action = "Index", id = urlparameter. optional}, // default value of the Parameter
             New String [] {"mvcdemo. controllers"} // namespace
         );

      URL: http: // localhost: 0000/Custom/1-detials

      This example is a routing rule with a namespace, which is useful in aeras. Let's talk about it later!

Example 4: routing rules with Constraints

     Function header:Maproute (string name, string URL, object defaults, object constraints); // route name, URL rule, default value, namespace

     Routes. maproute (
               "Rule1 ",
               "{Controller}/{action}-{year}-{month}-{day }}",
               New {controller = "home", Action = "Index", year = "2010", month = "04", Day = "21 "},
               New {year = @ "^ \ D {4}", month = @ "\ D {2 }"}
           );

      URL: http: // localhost: 14039/home/index-2010-01-21

Example 5: routing rules with namespace, constraints, and default values

     Function header:Maproute (string name, string URL, object ults, object constraints, string [] namespaces );

       Routes. maproute (
               "Rule1 ",
               "Admin/{Controller}/{action}-{year}-{month}-{day }",
               New {controller = "home", Action = "Index", year = "2010", month = "04", Day = "21 "},
               New {year = @ "^ \ D {4}", month = @ "\ D {2 }"},
               New String [] {"mvcdemo. controllers "}
           );

       URL: http: // localhost: 14039/Admin/Home/indexes-2010-01-21

Instance 6: capture all routes

      Routes. maproute (
               "All", // route name
               "{* Vauler}", // URL with Parameters
               New {controller = "home", Action = "Index", id = urlparameter. optional} // default value of the Parameter
           );
          

Description of the remaining parts of global. asax:

      Routes. ignoreroute ("{resource}. axd/{* pathinfo}"); is the URL for ignoring this rule

 

Arearegistration. registerallareas (); // register all areas
      Registerroutes (routetable. routes); // register the rules we have written
      // Routedebug. routedebugger. rewriteroutesfortesting (routetable. routes); // for debugging statements, download routedebug. dll and add references! After adding this sentence, you can test the URL routing.

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.