Register routes in area registration

2019-05-01 02:59发布

问题:

I have two routes in my area, one custom and one default fallback route, see below

        var dashboardRoute = new DashboardRoute(
            ObjectFactory.GetInstance<PathResolver>(),
            ObjectFactory.GetInstance<VirtualPathResolver>(),
            null);
        context.Routes.Add(dashboardRoute);

        context.Routes.MapRoute(
            "Dashboard_Default", // Route name
            "dashboard/{controller}/{action}/{id}", // URL with parameters
            new { controller = "pages", action = "index", id = UrlParameter.Optional, area = "Dashboard" } // Parameter defaults
        );

when I add both routes using context.Routes.Add/MapRoute the last route is not working, but when I use context.MapRoute on the last route it works but the GetVirtualPath in my custom route is not used for actionlinks. I thought that MapRoute was just an extension to context.Routes.Add? What is the best way to debug routes? I have used Phil Haacks route debug but it does not work with my custom route, is there any other way to debug routes?

I really need some help here. My route registrations in my dashboard area looks like this -

        var dashboardRoute = new PagesRoute(
            ObjectFactory.GetInstance<PathResolver>(),
            ObjectFactory.GetInstance<VirtualPathResolver>(),
            null);
        context.Routes.Add("Dashboard", dashboardRoute);

        context.MapRoute(
            "Dashboard_default",
            "dashboard/{controller}/{action}/{id}",
            new { controller = "dashboard", action = "index", id = UrlParameter.Optional }
        );

The PageRoute is a custom route and you can find the code here http://bit.ly/er6HPn With this routes active a link like this works great Html.ActionLink("Manage Roles", "manageroles", "account") but when I have a link that should work with my custom route like this Html.ActionLink(page.MetaData.Name, "edit", "pages", new { document = page },null) the result is http://stormbreaker.local/dashboard/pages/edit?document=Stormbreaker.Example.Models.Page, this means that GetVirtualPath in my PageRoute is never used. Can anyone explain to me way and how I could fix this?

回答1:

Solved it, If you register a custom RouteBase instance in the RouteCollection object, IRouteWithArea interface lets you associate that RouteBase instance with an area