ASP. NET MVC and WebForm each have their advantages, and we may need to use both ASP. NET MVC and WebForm. This article describes how to use WebForm in an ASP. NET MVC project.
Create a new folder named WebForms to hold WebForm, and add a Web Form file Demo.aspx as a demo.
Demo.aspx simply output a word "It's a WebForm."
The key step is the routing settings. If you want to webforms this folder name as part of the URL, that is, the normal WebForm application to access this demo.aspx, then simply ignore the routing rule.
Add the following code to the RegisterRoutes method of the Global.asax.cs file
Ignores route routes to the WebForms path. Ignoreroute ("Webforms/{weform}");
Results:
If you want the URL to be more friendly or do not appear webforms this folder name, it is necessary to write a class to inherit Iroutehandler.
1public class Webformsroutehandler:iroutehandler 2{ private String pageName = String. Empty; 5 Public IHttpHandler Gethttphandler (requestcontext requestcontext) { //Get the page parameter from the URL pageName = RequestContext.RouteData.GetRequiredString ("page");/ /Create instance //stitch into similar/webforms/according to page parameters Page.aspx Address to Access WebForms page IHttpHandler hander = Buildmanager.createinstancefromvirtualpath ("/webforms/" + This.pagename+ ". aspx", typeof (System.Web.UI.Page)) as ihttphandler;14 return hander; } 17}
then add a new routing rule to the Global.asax.cs file
Add a route to process with Webformsroutehandler//The portion of the URL where {page} is used as a parameter in Webformsroutehandler Routes.add (the new route ("web/{ Page} ", New Webformsroutehandler ());
when the path matches web/{page}, a custom class is used to handle the request, such as a URL such as Web/demo or WEB/DEMO1, which will match the route