Lyise
Lyise

Reputation: 1110

Routing to an aspx page on a project with webforms and MVC

I've added MVC to an existing webforms project that I've been working on as I'm looking to slowly migrate the project, however, during the change over period I need to be able to access both, but have it default to the aspx pages.

When it comes to registering the routing, I currently have this in place:

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

        routes.MapPageRoute("Backup", "{*anything}", "~/Default.aspx");

        routes.MapRoute(
            "MVC", 
            "{controller}/{action}/{id}", 
            new { controller = "Test", action = "Index", id = 1 }
        );
    }

However, when I have it setup like this, even if I put in a URL that I know refers to a controller/action combination (e.g. Test and Index from the last line of this), it still redirects the user to the Default.aspx page.

I've tried changing this over to the following:

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

        routes.MapRoute(
            "MVC",
            "{controller}/{action}/{id}",
            new { controller = "Test", action = "Index", id = 1 }
        );

        routes.MapPageRoute("Backup", "{*anything}", "~/Default.aspx");
    }

Here, the user can specify a page or specify a controller/action, but if they do not include any controller/aspx page, it defaults to the default MVC route, whereas I need it to default to the webform Default.aspx.

How would I get around the situation so that if no page/controller is specified, it directs to ~/Default.aspx, and if a controller is specified, it directs to that controller?

Upvotes: 3

Views: 4509

Answers (1)

Prerak K
Prerak K

Reputation: 11205

I tried experimenting with your scenario where in i have an existing web forms application and have added mvc to it. Basically i have come across two solutions.

First: you can ignore adding routes.MapPageRoute to route config; this is what i have done

public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.EnableFriendlyUrls();

            routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Please notice that i have excluded value for controller in the defaults. Now even if there is a match and if there is not controller present by that name then the request will fall back to the regular ASP.Net pipeline looking for an aspx page. With this approach you could request urls like http://localhost:62871/ or http://localhost:62871/Home/Index or http://localhost:62871/Home/ or http://localhost:62871/About.aspx

Second:: In this approach you can create an area under Areas folder for your new MVC based work and leave the RouteConfig in App_Start to its default so that it looks like following.

public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.EnableFriendlyUrls();
        }
    }

And then in your Area registration class define route as in following example .

public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Hope that helps. Thanks.

Upvotes: 4

Related Questions