ASP. net mvc and WebForm have their own advantages. We may need to use ASP. net mvc and WebForm at the same time. This article describes how to use WebForm in ASP. net mvc projects.
Create a folder named WebForms to store WebForm and add Demo. aspx as the Demo file.
Demo. aspx simply outputs a sentence "It's a WebForm ."
The key step is route settings. If you want the WebForms folder name to be part of the URL, that is, the normal WebForm application to access this Demo. aspx, you just need to ignore this routing rule.
Add the following code to the RegisterRoutes method of the Global. asax. cs file:
// Ignore the route to the WebForms path
Routes. IgnoreRoute ("WebForms/{weform }");
Result:
If you want the URL to be more friendly or without the WebForms folder name, you must write a class to inherit IRouteHandler.
1 public class WebFormsRouteHandler: IRouteHandler
2 {
3 private string pageName = string. Empty;
4
5 public IHttpHandler GetHttpHandler (RequestContext requestContext)
6 {
7 // obtain the page parameter from the URL
8 pageName = requestContext. RouteData. GetRequiredString ("page ");
9
10 // create an instance
11 // construct a URL similar to/WebForms/page. aspx Based on the page parameters to access the WebForms page
12 IHttpHandler hander = BuildManager. CreateInstanceFromVirtualPath ("/WebForms/" + this. pageName + ". aspx", typeof (System. Web. UI. Page) as IHttpHandler;
13
14 return hander;
15}
16
17}
Then add the new routing rule to the Global. asax. cs file.
// Add a route for processing with WebFormsRouteHandler
// The portion of the URL {page} is used as a parameter in WebFormsRouteHandler.
Routes. Add (new Route ("web/{page}", new WebFormsRouteHandler ()));
When the path matches web/{page}, a custom class is used to process this request. URLs such as web/demo or web/demo1 will match this route.
Sample download
This article applies to ASP. net mvc 1.0