RoutingProgramSystem. Web. routing is located in. NET Framework 3.5 SP1 and is separated from ASP. net3.5 MVC. Therefore, routing can also be used in traditional web form projects.
ASP. NET routing allows you to process URL requests that are not mapped to physical files in Web applications. By default, ASP. NET routing is enabled in an ASP. NET application of dynamic data or MVC framework, but routing is not enabled in the ASP. NET Website project.
Therefore, to use routing in ASP. NET websites, you must take measures to enable routing.
To implement routing in webform, you must first create a webformroutehandler class that implements the iroutehandler interface, and then configure the route ing in the global application class.
WebformroutehandlerCodeAs follows:
Using System. Web; Using System. Web. compilation; Using System. Web. Routing; Using System. Web. UI; Namespace Mvcwebapplication1 { Public Class Webformroutehanlder: iroutehandler { Public String Virtualpath { Get ; Private Set ;} Public Webformroutehanlder ( String Virtualpa) {virtualpath = virtualpa ;} Public Ihttphandler gethttphandler (requestcontext) {var page = buildmanager. createinstancefromvirtualpath (virtualpath, Typeof (Page )) As Ihttphandler; Return Page ;}}}
Configure the route in global. asax:
Using System; Using System. Web. Routing; Namespace Mvcwebapplication1 { Public Class Global: system. Web. httpapplication { Protected Void Application_start ( Object Sender, eventargs e) {registerroutes (routetable. routes );} Public Static Void Registerroutes (routecollection routes) {routes. Add (" Named ", New Route (" Foo/Bar ", New Webformroutehanlder (" ~ /Forms/Blech. aspx "); Routes. Add (" Number ", New Route (" One/two/three ", New Webformroutehanlder (" ~ /Forms/Haha. aspx ")));}}}
You also need to configure the reference of system. Web. Routing in Web. config!
<Httpmodules><Add Name="Urlroutingmodule" Type="System. Web. Routing. urlroutingmodule, system. Web. Routing, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/></Httpmodules>
Run and access http: // localhost: 5598/Foo/bar. OK ~~~~
Reference: msdn, MVC Architecture and actual address: How to: msdn helps use routing for Web Forms