The previous article introduced the document circulation System implementation, this article introduced under MVC under the dynamic custom route implementation.
In a typical CMS system, it is usually necessary for a column to specify a friend chain address, through the designated friend chain address, the column address more user-friendly, convenient memory, but also conducive to search engine optimization.
In MVC, however, it is often necessary to register a routing rule at application startup, which is often associated with a controller, where an address usually has a corresponding controller to handle. But how do you customize dynamic routing in MVC so that you can handle some runtime dynamically set URL addresses at run time with a controller?
The method of course is:
1, first implement a dynamic routing constraint class:
This class is used to determine whether the specified routing address satisfies a particular routing rule, which, of course, needs to be registered later to take effect.
1 Public classCmsurlconstraint:irouteconstraint2 {3 PublicCmsurlconstraint ()4 {5 6 }7 /// <summary>8 ///dynamically querying the database to determine whether the current request address satisfies the contract9 /// </summary>Ten /// <param name= "HttpContext" ></param> One /// <param name= "route" ></param> A /// <param name= "parametername" ></param> - /// <param name= "values" ></param> - /// <param name= "routedirection" ></param> the /// <returns></returns> - Public BOOLMatch (HttpContextBase HttpContext, Route route,stringparametername, routevaluedictionary values, - routedirection routedirection) - { + if(Values[parametername]! =NULL) - { + //This is the friend link address parameter for the request A varPermalink =Values[parametername]. ToString (); at - varDbContext =NewYbobjectcontext ("ybrapidsolution"); - //according to the friend link address parameter, find the column, the existence indicates the match succeeds - varpage = DbContext. Set<cmscolumn>() -. Where (p = = P.permalink = =permalink). FirstOrDefault (); - if(Page! =NULL) in { - //match successfully data transfer value tohttpcontext.current.items["Cmspage"] =page; + return true; - } the return false; * } $ return false;Panax Notoginseng } -}
2, then register the route cmscolumnroute and Cmsroute, specify the address of the above conditions to meet the constraints by which real controller to handle:
1 Public Static voidregisterroutes (routecollection routes)2 {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 //routes. Ignoreroute ("{ueditor/controller}.ashx{*pathinfo}");5Routes. Ignoreroute ("{Resource}.ashx/{*pathinfo}");6 7 routes. MapRoute (8Name"Cmscolumnroute",9Url:"{*permalink}",TenDefaultsNew{controller ="Home", action ="Columnpage", id =urlparameter.optional}, OneConstraintsNew{permalink =Newcmsurlconstraint ()} A ); - routes. MapRoute ( -Name"Cmsroute", theUrl:"{Action}/{*permalink}", -DefaultsNew{controller ="Home", action ="Columnpage", id =urlparameter.optional}, -ConstraintsNew{permalink =Newcmsurlconstraint ()} - ); + - routes. MapRoute ( +Name"Default", AUrl:"{Controller}/{action}/{id}", atDefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} - ); -}
Operating principle:
1. Enter a specific address
2, the system through the match method of the Cmsurlconstraint class query database, such as the address and a column set in the same friend chain address, it means that the match is successful
3. If the match succeeds, then the Controller and action set by registering the route are processed and returned to view.
The process is as follows:
The implementation results are as follows:
1, the operation process can be arbitrarily designated the address of the column:
2. The page is then accessible through the specified address, but we do not have a controller that specifically implements news:
Ybsoftwarefactory code Generation Plug-in "24": Implementing dynamic Custom Routing in MVC