Use WebForm in ASP. net mvc Project

Source: Internet
Author: User

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

 

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.