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.