ASP. MVC5 Pseudo-Static WebForm
Mvc4 and 5 Universal
1, background: The old project WebForm development needs to integrate into the new project MVC5 development
2, demand: URL address truckdetail.aspx?id=455 reach the effect truck/455.html
3, not the best solution, but an over-project so to achieve the effect OK, welcome to lose.
First step: Inherit Iroutehandler
public class Oldwebformsroutehandler:iroutehandler { private string pageName = String. Empty; private string id = string. Empty; Public IHttpHandler Gethttphandler (RequestContext requestcontext) { PageName = RequestContext.RouteData.GetRequiredString ("Oldpagename"); id = requestContext.RouteData.GetRequiredString ("id"); String Path= "/pages/" + This.pagename + ". aspx"; IHttpHandler Myhander = Buildmanager.createinstancefromvirtualpath (path, typeof (System.Web.UI.Page)) as IHttpHandler ; return myhander; } }
Step Two: Add note before the default rule. html
public class Routeconfig {public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. ADD ("Myroute", New Route ("{oldpagename}/{id}.html", new Oldwebformsroutehandler ())); Routes. MapRoute ( name: "Default", URL: "{controller}/{action}/{id}", defaults:new {controller = "Home", action = "Index", id = urlparameter.optional} ); } }
Step Three: Modify the Web. config
<system.webServer> <modules runallmanagedmodulesforallrequests= "true"/></system.webserver >
Fourth step: aspx page get parameter request.querystring["id" replaced with routedata.values["id"]
<form id= "Form1" runat= "Server" > <div> id:<%=routedata.values["id"]==null? " YY ": routedata.values[" id "]. ToString ()%><br/> oldpagename:<%=routedata.values["Oldpagename"]==null? " No ": routedata.values[" Oldpagename "]. ToString ()%> </div></form>
Effect:
Reprint please indicate source address: http://www.cnblogs.com/huangyoum/p/4154338.html
ASP. MVC5 Pseudo-static