"Go" Using WebForm in an ASP. NET MVC Project

Source: Internet
Author: User

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



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.