I had an ASP.NET MVC4 app with the following routing defined in the Global.asax.cs. The app's start page is Index.cshtml view that is returned from the action method Index() of the Home controller. I then added a legacy ASP.NET WebForms app that had Default.aspx as the start page. How can I make this Default.aspx page as the start page of this integrated MVC+WebForms app:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}.mvc/{action}/{id}",
new { action = "Index", id = "" }
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.
Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table. In MVC 5. if you have a form login, when you click login on the home page, it will then still redirect to Home controller , not your custom controller specified in the route.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.
If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks.
Try the following in your Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Web Forms default
routes.MapPageRoute(
"WebFormDefault",
"",
"~/default.aspx"
);
// MVC default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // parameter default
);
}
Also I don't think you'll need the .mvc
portion of the Default
route.
Removing 'controller = "Home"' from defaults parameter worked great for me. I found that solution here: https://stackoverflow.com/a/21213711/9793076.
Change the defaults parameter from:
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
to:
new { action = "Index", id = UrlParameter.Optional }
The current solution breaks the routing of MVC links as @nam said.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With