In MVC, the address that the user accesses does not map to the corresponding file in the server, but maps to the corresponding actionmethod in the corresponding control, by Actionmethod to decide what kind of information to return the user. The routing system completes the work of addressing the address of the user to the corresponding action (or, of course, the corresponding file), and many of these complex processes are completed automatically by. NET, and the developer needs to tell the. NET user the specific mapping relationship between the access address and the corresponding action. The MVC middle system can accomplish two tasks: 1, processing from the user received the URL, mapping to the corresponding action 2, an action based on the routing system Mapping relationship, reflected in the routing system to match the structure of the URL (dynamically generated URLs, when the site structure changes, the URL will also Automatic change);
One, processing incoming URL: The specific mapping relationship of the route is defined in the Global.aspx file: public static void RegisterRoutes (RouteCollection routes) {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 = "List", category = (string) null}, new {page = @ "\d+"}//Con Straints: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        &NBsp; new {controller = "Product"  , Action = " List ", page = 1}, new {page = @ "\d+" } //constraints:page must be numerical & nbsp; );
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, the Application_Start () method invokes the RegisterRoutes method, and the developer's custom route map is in the RegisterRoutes method. 1, customize Route and add to routetable: Route myroute = new Route ("{controller}/{action}", New Mvcroutehandler ());
Routes. ADD ("Myroute", Myroute);
 2, through the Routes.maproute method (its internal still customizes a route added) routes. Maproute ( ) Routename ", //The first parameter is specified as the name of the new route; "Abc{category}/{controller}/page{page}/def", //second parameter is the main mapping relationship new&nBsp {Controller = "Product"  , Action = "List", page = 1}, //The third parameter is the default parameter and is based on this default parameter when the mapping does not match actio Matching new {of n Page = @ "\d+"  } &NBS P //The fourth parameter is the relevant restriction condition, where the page parameter must Must be digital new {"NameSpace"} &N Bsp &NB Sp //The fifth parameter is a namespace, that is, the route matching is valid under that namespace ); The second parameter, "Abc{category}/{controller}/page{page}/def", the routing system will match the URL of the user request and the parameters here according to the parameters here. There are two kinds of imagesShooting match: ① dynamic matching, placed in {}, that is, to match the parameter names, such as Category,controller,page,url, after ABC, the first/previous content will be matched and "assignment" Give category parameters. Similarly, the second/to third/the content will be matched to the value of the controler parameter, i.e. the corresponding actionmethod should be found in this controller. ② Static match, placed outside {} content, will be here every word Fu Tong URL comparison, such as Abc.../.../page..../def If both the URL and the ① and ② of the parameter match each other successfully, then the exact match is successful, and the corresponding controller and action are matched by the URL, otherwise the successful item is not matched: for example, page will match the action by the default parameter, 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 the match succeeds. In the design of route matching, we should pay attention to the sequence of matching; Secondly, the second function of converting the corresponding action to the URL routing system is to convert an action to the corresponding URL. Of course, the static URL can be written in the code, but when the site's routing mapping system changes, the static URL needs to be all modified, the use of the routing system can be dynamic conversion 1, generate links:<a> @Html. ActionLink ("About this appli Cation "," Index "," Home ", new {id =" Myanchorid ", @class =" Mycssclass "}) the fourth parameter can provide attributes for the generated link; QueryString is generated when the supplied parameter is inconsistent with the parameters in the routing system:?.. =.. 2, generate the URL string: Only the string that produces the URL, that is, the content after the href @Html. Action (), the usage is consistent with ActionLink.
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.