Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 with webforms Default.aspx as the start page

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 = "" }
            );


        }
like image 698
nam Avatar asked Feb 17 '14 01:02

nam


People also ask

Can you mix webforms and MVC?

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.

How do I change the default page in ASP.NET MVC?

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.

Which of the following is a key difference between asp net webforms and ASP.NET MVC processing?

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.

Can you use ASPX pages in MVC yes or no?

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.


2 Answers

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.

like image 63
Brent Mannering Avatar answered Oct 14 '22 07:10

Brent Mannering


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.

like image 22
emilsteen Avatar answered Oct 14 '22 08:10

emilsteen