Skip to content

Area Routes Overrides Default Route #17

Description

@harouny

Hi Daniel,
We have faced the following issue:
In MvcApplication.RegisterRoutes we have this route definition:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

And we have an Area with the name (Reports) and this route definition:

context.MapRoute(
    "Reports_default",
    "Reports/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

On client if we use:

Router.action("Tag", "Create", { offeringId: 1})

Its outputting: "/site/Reports/Create/Tag?offeringId=1"
however, it should output: "/site/Tag/Create?offeringId=1"

We fixed this by adding this bit of code after including routeJs script:

Router.route = function(routeValues) {
    for (var i = 0, count = this.routes.length; i < count; i++) {
       // fix  Area Routes Overriding Default Route
       if (this.routes[i].route.defaults.area != routeValues.area)
        continue;
       // end
        var url = this.routes[i].build(routeValues);
        if (url) {
           return this.baseUrl + url;
        }
     }
     throw Error('No route could be matched to route values: ' + routeValues);
};

This will fix the issue
And we are thinking that in case we needed to get the action URL for the Area we will add area=areaName to route parameters like this:

Router.action("Reports", "List", { area : 'Reports'})

The question is are we following the correct approach here? is there any other way of doing this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions