As one of ASP.NET developers ever asked to upgrade an existing ASP.NET WebForms 3.5 application to ASP.NET 4 and added ASP.NET MVC pages to it, I found the tip, Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms Applications from Scott Hanselman very helpful.
After I followed all the steps provided, I was still baffled by the inconsistent behavior of the web app itself. When the app is run on ASP.NET Development Server locally, the default landing page is my default.aspx, which is the intended result. For some reason, every time I deploy it to production, the default changes to the Index action in MVC 3 HomeController.
It must be the combination of me being a novice in MVC 3 and ASP.NET routing altogether that I had to struggle with this issue for at least a few days before finding out that the solution actually was as simple as reversing the order of my ASP.NET routing.
Here’s what I originally had in my Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}"); // MVC default routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); // Web Forms default routes.MapPageRoute( "WebFormDefault", "", "~/default.aspx"); }
Here’s what I have now that fixes the issue on the production:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{myWebForms}.aspx/{*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 defaults ); }
So that was the only thing I needed to do to fix the problem. The only unsolved mystery for me is I still have no answer on why the ASP.NET Development Server seems to default to the web forms automatically while the IIS 7.5 is more nitpicky about it.
not Global.asax.cs but RouteConfig.cs