Routing system in MVC3 (Routes)

Source: Internet
Author: User
Tags actionlink

Reprint: http://blog.csdn.net/francislaw/article/details/7429317

In MVC, the address that the user accesses does not map to the corresponding file in the server, but instead maps to the corresponding actionmethod in the corresponding control and is determined by the Actionmethod to return the user what information. The routing system completes the work of addressing the address of the user to the corresponding action (which can also be the corresponding file), many of which are done automatically by. NET, and the developer needs to tell the. NET user's access address and the specific mapping of the corresponding action.

The MVC road system can accomplish two tasks:1, processing the URL received from the user, mapped to the corresponding action;2, an action according to the mapping relationship of the routing system, reflected into the URL matching the structure of the routing system (dynamically generated URL, when the site structure changes, the URL will also automatically change);
one, the URL to handle incoming:The specific mapping of a route is defined in the Global.aspx file:Public static void registerroutes ( routecollection routes) /c9>         {routes. Ignoreroute ( "{resource}.axd/{*pathinfo}" );
routes. MapRoute ( null, " ",///null route New {controller = "Product" , action = "List", category = ( string null , page = 1}, New[] { "SportsStore.WebUI.Controllers" }             );
routes. MapRoute ( null, "Page{page}", //Matches/page2,/page123, but not/pagexyz                  new  {controller =  "Product"  , action = string) null  New {page = @ "\d+" } //Constraints:page must be numerical             );
routes. MapRoute ( null, "{category}", //Matches/football or/anythingwithnoslash New {controller = "Product" , action = "List", page = 1}             );routes. MapRoute ( null, "{category}/page{page}", //Matches/football or/anythingwithnoslash/page1 New {controller = "Product" , action = "List", page = 1}, New {page = @ "\d+" } //Constraints:page must be numerical            );
routes. MapRoute ( "Default", //Route name ' {controller}/{action}/{id} ' , //URL with parameters New {controller = "Product" , action = "List", id = Urlparameter . Optional} //Parameter defaults             );
        }when the system starts, theApplication_Start () method calls the RegisterRoutes method, and the developer-defined route map is in the RegisterRoutes method. 1. Customize the route and add it to routetable:Route Myroute = new Route ("{controller}/{action}", New Mvcroutehandler ());
Routes. ADD ("Myroute", Myroute);
2, through the Routes.maproute method (which still internally customizes a route add) routes. MapRoute (                  ,                                                                               //The first parameter is specified as the name of the new route; "Abc{category}/{controller}/page{page}/def",//The second parameter is the primary mapping relationship New {controller = "Product" , action = "List", page = 1},//The third parameter is the default parameter , the action is matched against this default parameter when the mapping does not match                  new  {page = //The fourth parameter is a related constraint, which means that the page parameter must be a number The new {"NameSpace"}//fifth parameter is a namespace, that is, the route matches the Namespaces are valid under a single namespace            );in the second parameter, "Abc{category}/{controller}/page{page}/def", the routing system will match the URL of the user request with the parameters here, according to the parameters here, there are two ways to match the map:① Dynamic Matching, placed in {}, is to match the name of the parameter, such as Category,controller,page,url, after ABC, the first/previous content will be matched and "assigned" to the category parameter. Similarly, the second/to third/to the content will be matched to the value of the Controler parameter, that is, the controller needs to find the corresponding actionmethod. ② A static match, placed outside of {}, compares each word descriptor URL here, such as Abc.../.../page..../defif the URL and the ① and ② of the parameter match successfully, the exact match succeeds, the corresponding controller and action are matched by the URL, otherwise the successful entry is not matched: for example, the page will match the action by default parameters. Throws an exception if the default parameter match is unsuccessful
when multiple routemap are registered, they are matched from top to bottom in the order in which they are registered, and no subsequent matches are found after a successful match. In the design of route matching, we should pay attention to the matching sequence;
two, convert the corresponding action to a URLthe second function of the routing system is the implementation of converting an action to the corresponding URL. Of course, you can write a static URL in the code, but when the site's routing mapping system changes, the static URL needs to be fully modified, using the routing system can achieve dynamic conversion1, Generate link:<a>@Html. ActionLink ("About this application", "Index", "Home", new {id = "Myanchorid", @class = "Mycssclass"}) the fourth parameter can be Provide properties for the generated link; QueryString is generated when the supplied parameters are inconsistent with the parameters in the routing system:?.. =..2, Generate URL string: A string that simply produces a URL, that is, the contents of the HREF@Html. Action (), usage is consistent with ActionLink.

Routing system in MVC3 (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.