Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Area Routes Overrides Default Route #17

Closed
harouny opened this issue Jul 23, 2013 · 4 comments
Closed

Area Routes Overrides Default Route #17

harouny opened this issue Jul 23, 2013 · 4 comments
Labels

Comments

@harouny
Copy link

harouny commented Jul 23, 2013

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?

@Daniel15
Copy link
Owner

Thanks for the bug report, I'll try to look into this over the weekend. Your approach looks correct to me but I think the logic should be in the Route.prototype.build method instead.

@joshka
Copy link

joshka commented Jul 26, 2013

@Daniel15 You're definitely correct. It should be in the build method. For the workaround though, I was unable to get the 'this' object to correctly point at the right route when attempting to override the Route.prototype.build method. We were writing this after the routejs script tags mostly a workaround until a proper fix was in. Thanks for your quick reply.

@harouny
Copy link
Author

harouny commented Jul 26, 2013

@Daniel15 Thanks for the quick replay

@Daniel15
Copy link
Owner

That Route.prototype.build method is very messy (way too long), I need to clean it up a bit.

This bug has been fixed in the 1.1.1 release which I've just pushed to NuGet. Let me know if you encounter any other problems :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants