Mixed Development of MVC and Webform

Source: Internet
Author: User

In actual projects, we will think of Asp.net MVC as the frontend and WebForm as the background. Both performance and development efficiency. It is also easy to implement mixed development between the two in MVC.

Here we will introduce two methods:

First, set the route to ignore the access control on the. aspx file of WebForm.

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

routes.MapRoute(
"Account",
"Account/{action}",
new { controller = "LoinAccount", action = "Index" }

);

routes.MapRoute(
"ads",
"Ads/{action}/{id}/{num}",
new { controller = "Ads", action = "ListAd" },
new { num = @"\d+" }
);

routes.MapRoute(
"News",
"News/Get_{newsId}/{year}/{month}/{day}.htm",
new { controller = "News", action = "Index" }
);

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);

}

Second: implement a RouteHandler

/// <Summary>
/// Process the path of the WebForms folder
/// </Summary>
Public class WebFormsRouteHandler: IRouteHandler
{
Private string _ pageName = string. Empty;


Public IHttpHandler GetHttpHandler (RequestContext requestContext)
{
// Obtain the page parameter from the URL
_ PageName = requestContext. RouteData. GetRequiredString ("page ");
IHttpHandler hander = BuildManager. CreateInstanceFromVirtualPath ("/WebForms/" + this. _ pageName + ". aspx", typeof (System. Web. UI. Page) as IHttpHandler;
Return hander;
}
}

/// <Summary>
/// Process the sub-Folder path in WebForms
/// </Summary>
Public class WebFormsFolderRouteHandler: IRouteHandler
{
Private string _ folderName = string. Empty;
Private string _ pageName = string. Empty;

Public IHttpHandler GetHttpHandler (RequestContext requestContext)
{
// Obtain the folder parameter from the URL
_ FolderName = requestContext. RouteData. GetRequiredString ("folder ");
// Obtain the page parameter from the URL
_ PageName = requestContext. RouteData. GetRequiredString ("page ");

IHttpHandler hander;
// Create an instance
// Construct a URL similar to/WebForms/folder/page. aspx according to the folder and page parameters to access the WebForms page
Hander = buildmanager. createinstancefromvirtualpath ("/webforms/" + this. _ Foldername + "/" + this. _ pagename + ". aspx ", typeof (system. web. UI. page) as ihttphandler;
Return hander;

}
}

Next, create a webforms folder and add the ASPX page to its folder or subfolders.

Finally, set the route

Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");

// 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 ()));
// The contents of {folder} and {page} in the URL are used as parameters in webformsroutehandler.
Routes. Add (new route ("Web/{folder}/{page}", new webformsfolderroutehandler ()));

Routes. maproute (
"Account ",
"Account/{action }",
New {controller = "loinaccount", Action = "Index "}

);

Routes. maproute (
"Ads ",
"Ads/{action}/{ID}/{num }",
New {controller = "ads", Action = "listad "},
New {num = @ "\ D + "}
);

Routes. maproute (
"News ",
"News/get _ {newsid}/{year}/{month}/day1_.htm ",
New {controller = "news", Action = "Index "}
);

Routes. maproute (
"Default ",
"{Controller}/{action}/{ID }",
New {controller = "home", Action = "Index", id = ""}
);

}

In this way, if you think the URL is not beautiful enough, you can modify the path in the route to make the URL look better.

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.