Dresel / RouteLocalization

RouteLocalization is an MVC and Web API package that allows localization of your attribute routes.
MIT License
67 stars 13 forks source link

Multiple Routes found for given Controller and Action. Narrow down your selection or use AddTranslationToSimiliarUrls if you want to translate similiar Routes at once. #32

Closed brgrz closed 10 years ago

brgrz commented 10 years ago

After upgrading to v.2.1.0 I am now getting this exception for apparent no reason:

Multiple Routes found for given Controller 'Directory' and Action 'List'. Narrow down your selection or use AddTranslationToSimiliarUrls if you want to translate similiar Routes at once.

If I set the AddTranslationToSimiliarUrls to true in configuration, it just results in another exception:

Multiple Routes with different Url found for given Controller 'Directory' and Action 'List'. Narrow down your selection.

The issue could be connected to the fact I use T4MVC in my projects, which adds additional parameterless routes to routes that have parameters.

However, this issue was not present in 2.0.0-alpha-6

brgrz commented 10 years ago

This apparently happens because I have two routes defined on a single controller action:

[Route("{*path}", Order=0)] [Route("categories/list/{*path}", Order=1)]

Though I don't quite get it why it wouldn't be allowed here:

        `if (localizationCollectionRoutes.Count == 1)
        {
            return localizationCollectionRoutes;
        }

        if (!Configuration.AddTranslationToSimiliarUrls)
        {
            throw new InvalidOperationException(
                string.Format(
                    "Multiple Routes found for given Controller '{0}' and Action '{1}'." +
                        " Narrow down your selection or use AddTranslationToSimiliarUrls if you want to translate similiar Routes at once.",
                    controller, action));
        }

        LocalizationCollectionRoute localizationCollectionRoute = localizationCollectionRoutes.First();

        if (localizationCollectionRoutes.Any(x => x.Url() != localizationCollectionRoute.Url()))
        {
            throw new InvalidOperationException(
                string.Format(
                    "Multiple Routes with different Url found for given Controller '{0}' and Action '{1}'." +
                        " Narrow down your selection.", controller, action));
        }`
Dresel commented 10 years ago

Normally you would not want to translate multiple routes at once when the url differs (thats why AddTranslationToSimiliarUrls does not work here). In previous versions in this case the first route was translated - this could and already did lead to confusions so I added this check.

For this use case you could add (one or if you want to translate both two) named routes.

// Route definition
[Route("{*path}", Order=0, Name="Name1")]
[Route("categories/list/{*path}", Order=1, Name="Name2")]
ActionResult MyAction(string path)

// Localization
localization.ForNamedRoute("Name1").AddTranslation ...
localization.ForNamedRoute("Name2").AddTranslation ...

If you have a better idea to handle this situations I'm open for suggestions.

brgrz commented 10 years ago

The new version is not working fine for me at all, various issues, will have to dig into it and troubleshoot. I will let you know what I've discovered.

Dresel commented 10 years ago

If you post your action definition and RouteLocalization configuration and the expected / received result I'm sure we can clarify those issues. The changes from 2.0.0-alpha-6 to 2.1.0 are mostly changes to RouteArea / RoutePrefix handling (see Translating RoutePrefix and RouteArea).

brgrz commented 10 years ago

After removing the 2nd route above, rebuilt and rerun, no route gets hit. That's with v.2.1. The exact same code works fine and defaults to one default content route with version 2.0.0-alpha-6.

Which were the commits that introduced these changes?

brgrz commented 10 years ago

Somehow things got working by rebuilding again. I also made some progress using your Glimpse package.