Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register routes in area registration

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?

like image 964
marcus Avatar asked Jan 26 '11 20:01

marcus


People also ask

Where can routes be registered?

You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.

What is area registration?

Definition of registration area : the part of the U.S. having a public registration of births or deaths that meets the standards set by the Census Bureau and including more than 95 percent of the population of the U.S.

How can I register my area in MVC?

To add an area to an MVC application, right-click on the project item with in the Solution Explorer window and select Add>Area option as shown below. Now a new prompt will appear, with in give the name of the area like "Department" and click Add button.

How do you add area in VS 2022?

You can create an area by right-clicking on the project in the solution explorer -> Add -> Area.. , as shown below. Enter the name of an area in the Add Area dialogue box and click on the Add button.


1 Answers

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

like image 111
marcus Avatar answered Sep 30 '22 21:09

marcus