On the pseudo-static implementation of. NET MVC, there are many blog posts on the web, but neither can achieve true pseudo-static, or adapt to more complex pseudo-static requirements, such as paging query page.
First, we want to write a route interceptor, which some of the interception rules processing, you need to deal with the actual situation, I just expanded the paging query pseudo-static processing, the code is as follows:
<summary>///Maximus Ye//email:[email protected]//qq:275623749//< /summary> public class Mvchtmlroute:routebase {public Mvchtmlroute () {} public ov Erride routedata Getroutedata (httpcontextbase HttpContext) {routedata result = null; String RequestedUrl = HttpContext.Request.AppRelativeCurrentExecutionFilePath + httpContext.Request.PathInf O list<string> urlitemlist = requestedurl.split ('/'). ToList (); if (Urlitemlist.count () ==3) {//missing action int extpos = urlitemlist[2]. IndexOf (". html"); int pagepos = urlitemlist[2]. IndexOf ("_"); if (Extpos > 0) {result = new Routedata (This, new Mvcroutehandler ()); Result. Values.add ("Controller", Urlitemlist[1]); Result. Values.add ("Action", "Index"); if (Pagepos > 0) {//paging parameter split result. Values.add ("id", urlitemlist[2]. Substring (0, Pagepos)); Result. Values.add ("page", Urlitemlist[2]. Substring (Pagepos + 1, extpos-pagepos-1)); } else {result. Values.add ("id", urlitemlist[2]. Substring (0, Extpos)); }}} return result; } public override Virtualpathdata GetVirtualPath (RequestContext requestcontext, routevaluedictionary values) {return null; } }
Then add our interceptors in Global.asax's RegisterRoutes method and add other pseudo-static rules, the code is as follows:
public static void RegisterRoutes (RouteCollection routes) {routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. ADD (New Lib.mvchtmlroute ()); Routes. MapRoute ("id_pagehtml",//ID pseudo-Static Extended paging parameter page "{controller}/{action}/{id}_{page}.html",//With reference Number of URL new {controller = "Home", action = "Index", id = urlparameter.optional, page = Urlparameter.optiona L}//parameter default value); Routes. MapRoute ("idhtml",//ID pseudo-static "{controller}/{action}/{id}.html",//URL with parameter n EW {controller = "Home", action = "Index", id = urlparameter.optional}//parameter default value); Routes. MapRoute ("actionhtml",//action pseudo static "{Controller}/{action}.html/{id}",//URL with parameter New {controller = "Home", action = "Index", id = urlparameter.optional}//parameter default value); Routes. MapRoute ("controllerhtml",//Controller pseudo static "{Controller}.html/{action}/{id}",//URL with parameter new {controller = "Home", Action = "Index", id = urlparameter.optional}//parameter default value); The following warranty does not do pseudo-static processing of links can also be recognized routes normal. MapRoute ("Default", "{Controller}/{action}/{id}", new {controller = "Home", AC tion = "Index", id = urlparameter.optional}//parameter default value); Routes. MapRoute ("Root", "", new {controller = "Home", action = "Index", id = URLP Arameter. Optional});//root directory match}
OK, the above to get all done, the specific pseudo-static effect, you can access: www.etagrfid.com view.
Reprint please indicate the source, thank you!
. NET MVC3.0 Pseudo-static implementation