Understanding ASP. NET 3.5 MVC routing using routing in the webform Project

Source: Internet
Author: User

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

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.