The order of MVC routes

Source: Internet
Author: User

<1>

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using System.web.routing;namespace mvcapplication{public class Routeconfig {public static void RegisterRoutes (Rou Tecollection routes) {routes.            Ignoreroute ("{resource}.axd/{*pathinfo}"); The routes are sequential. Ranked at the very beginning of the first is selected. If the match is not on the second bar to start the selection ... Until the end is matched up//If I am now another Home controller index method. Take the following two routing rules test//test 1 Enter Http://localhost:2006/Home/Index in the browser//Because the default route is in front of the top, so it starts matching first because DEFAU            The URL rule of lt is url: "{controller}-{action}/{id}"//Because the URL of the controller I entered is followed by "/", and the default URL rule is followed by "-", so there is no match on this route.             Because the first route does not match, let's look at the second route DEFAULT2://Because the URL address controller that I entered is followed by "/", and the controller of the DEFAULT2 URL rule is followed by "/" so it matches. Match up after. Go to the Controller folder and look for the home controller. If you find it, keep looking for the index method. If one is found, the entire routing rule is passed. If one of the home controllers, or the index method is not found, then the error will be "unable to find resources."            "//Note: As long as the current route matches, the subsequent route will not continue to match. Test 2 Enter Http://localhost:2006/Hom in the browserE-index//Because the default routing rule is in the front, it first starts matching because the URL rule for default is URL: "{controller}-{action}/{id}"//Because I enter            The URL address controller is followed by a "-", and the DEFAULT2 URL rules are followed by "-", so the route is matched. Match up after. Go to the Controller folder and look for the home controller. If you find it, keep looking for the index method. If one is found, the entire routing rule is passed.             If one of the home controllers, or the index method is not found, then the error will be "unable to find resources." Routes. MapRoute (Name: "Default", url: "{controller}-{action}/{id}", Defaults:new {controll            ER = "Home", action = "Index", id = urlparameter.optional}); Routes. MapRoute (Name: "Default2", url: "{controller}/{action}/{id}", Defaults:new        {controller = "Home", action = "Index", id = urlparameter.optional}); }    }}


<2> for the above test, I will now swap the Default route with DEFAULT2 for a position. Put DEFAULT2 to the front and see the following changes

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using System.web.routing;namespace mvcapplication{public class Routeconfig {public static void RegisterRoutes (Rou Tecollection routes) {routes.            Ignoreroute ("{resource}.axd/{*pathinfo}"); The routes are sequential. Ranked at the very beginning of the first is selected. If the match is not on the second bar to start the selection ... Until the end is matched up//If I am now another Home controller index method. Take the following two routing rules test//test 1 Enter Http://localhost:2006/Home/Index in the browser//because DEFAULT2 this routing rule is in front of it, so it starts matching first because def Ault2 URL rule is url: "{controller}/{action}/{id}"//Because the URL address controller I entered is followed by "/", and the controller of the DEFAULT2 URL rule is followed by "/", so this route matches the            。 Match up after. Go to the Controller folder and look for the home controller. If you find it, keep looking for the index method. If one is found, the entire routing rule is passed. If one of the home controllers, or the index method is not found, then the error will be "unable to find resources." "//Because the first route has already been matched.                      So the subsequent route will not continue to match. Test 2 Enter Http://localhost:2006/Home-Index//In the browser because this routing rule is in front of the DEFAULT2, so it starts matching first. Because the URL rule for Default2 is URL: "{controller}/{action}/{id}"            The point is that because the controller of a routing rule ends with a "/", the action method also ends with a/, and other parameters, such as the ID, end with a "/". Note: By default, the "controller" and "Action" methods of the URL can be omitted.            So in the browser input: http://localhost:2006 can also be parsed, it is the abbreviation of Http://localhost:2006/Home/Index. Focus 2: When we enter Http://localhost:2006/Home-Index in the browser, we "think" here the controller is the Home,action method is index. It does not//when we enter the Http://localhost:2006/Home-Index it will be "home-index" recognized as a controller. "Note: Controller and Action methods can be omitted" so the URL you entered is actually parsed into http://localhost:2006/Home-Index/Index//Since the DEFAULT2 's routing rules Then the controller is followed by the "/" and http://localhost:2006/Home-Index/Index the URL of the controller is followed by "/", so the match on//And it goes to the controller folder to find "Home-index" this Controller. The results were not found. So the error is "unable to find resources"//notes under current routing rules "If the controller here is" Test "the Action method is" Wowo "you enter Http://localhost:2006/Test-wowo in the browser. Your URL is actually parsed into a http://localhost:2006/Test-wowo/Index "routes. MapRoute (Name: "Default2", url: "{controller}/{action}/{id}", Defaults:new {controller = "Home", action = "Index", id = URlparameter.optional}); Routes. MapRoute (Name: "Default", url: "{controller}-{action}/{id}", Defaults:new {controll                                       ER = "Home", action = "Index", id = urlparameter.optional}); }    }}


The order of MVC routes

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.