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

前端 未结 1 1356
遥遥无期
遥遥无期 2021-01-06 12:12

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

相关标签:
1条回答
  • 2021-01-06 12:51

    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.

    0 讨论(0)
提交回复
热议问题