5

I'm having some trouble getting routes to match.

I am using base-32 encoded int's as short links to slideshows in a web app. There are 5 different versions of each slideshow, and I am using an initial letter to distinguish between each version.

The routes always match, except when the first character of the base-32 encoded int is the same as the character designating the slideshow version. This anomaly exists for all 5 prefix letters: n, f, c, x, and h.

What about the first two characters being the same makes these routes not match? I'm at a loss to understand what's going on here (when the routes don't match, they simply fall through to the default).

Route Matches (/na0): route for na0

Route Doesn't Match (/nn0): route for nn0

Route Matches (/nfg): route for nfg

Route Doesn't Match (/ffg): route for ffg

I'm boggled. Here's the routing code, in case it isn't clear in the RouteDebug tables in the screenshots:

        routes.MapRoute(
            "NonBrandedSlideshow",
            "n{id}",
            MVC.Slideshow.NonBranded(), null,
            new { id = Settings.Base32Regex }
        );

        routes.MapRoute(
            "FullSlideshow",
            "f{id}",
            MVC.Slideshow.Full(), null,
            new { id = Settings.Base32Regex }
        );

        routes.MapRoute(
            "CompactSlideshow",
            "c{id}",
            MVC.Slideshow.Compact(), null,
            new { id = Settings.Base32Regex }
        );

        routes.MapRoute(
            "FlexibleSlideshow",
            "x{id}",
            MVC.Slideshow.Flexible(), null,
            new { id = Settings.Base32Regex }
        );

        routes.MapRoute(
            "Html5Slideshow",
            "h{id}",
            MVC.Slideshow.NonBrandedHtml5(), null,
            new { id = Settings.Base32Regex }
        );

I should note here that I am using T4MVC (see section 2.2.5), and these MapRoute methods are extensions added by T4MVC. The MapRoute methods I am using are equivalent to the standard methods, and I have tried using the non-T4MVC MapRoute method with the same result:

        routes.MapRoute(
            "Html5Slideshow",
            "h{id}",
            new { controller = "Slideshow", action = "NonBrandedHtml5" },
            new { id = Settings.Base32Regex }
        );

Here is the Base32Regex, though I have tried it with and without this constraint (the Base32 implementation I am using will assume I and O are 1 and 0, for example).

public static partial class Settings
{
    public static string Base32Regex
    {
        get { return @"[0-9ABCDEFGHJKMNPQRSTVWXYZ]+"; }
    }
}
Max Toro
  • 28,282
  • 11
  • 76
  • 114
quentin-starin
  • 26,121
  • 7
  • 68
  • 86
  • Can you post the Base32 Regex? – epignosisx Jan 03 '12 at 12:20
  • @epignosisx: yes, I did. It is in the screenshots, but they did not come through at full size it appears. Note that I had the same result with and without the Regex constraint. – quentin-starin Jan 03 '12 at 12:26
  • @qes What tool is that you're using in FireFox for routes?? –  Jan 03 '12 at 13:53
  • @Shark: it's not a FireFox tool, it's Phil Haack's RouteDebugger library: http://haacked.com/archive/2011/04/13/routedebugger-2.aspx – quentin-starin Jan 03 '12 at 13:57
  • @mateuscb: ah, thanks for pointing that out. It's an extension method added by T4MVC. It's what allows me to use a strongly-typed method to access the action methods - `MVC.Slideshow.Flexible()`. I have tried the non-T4MVC `MapRoute` and receive the same results. – quentin-starin Jan 07 '12 at 19:18

5 Answers5

3

This is a known bug in ASP.NET Routing, see this answer for an explication and a workaround. In short, using literal sub-segments does not work very well.

Community
  • 1
  • 1
Max Toro
  • 28,282
  • 11
  • 76
  • 114
3

Well, I am just as boggled. I performed some other tests and as far as I can see, this has to be some bug in the way the routes are checked when the constant before the route parameter has the same repeated character. Some other boggling examples:

"nn{id}"
route matches (/nna0)
route doesn't match (/nnn0)

"nnn{id}"
route matches (/nnna0)
route doesn't match (/nnnn0)

as soon as i make the constant not repeat the same character, all is well

"mn{id}"
route matches (/mna0)
route matches (/mnn0)
route matches (/mnmn)

This may not be exactly what you are looking for. But given the oddity of the situation, it was the only thing I could come up with. Add the constant to the constraint and remove it from the url. Then in your controller (this is the part I didn't like) you will need to remove that constant from the id parameter. Hope this works, or at the least help spark another solution.

routes.MapRoute(
    "NonBrandedSlideshow",
    "{id}",
    MVC.Slideshow.NonBranded(), null,
    new { id = "n"+Settings.Base32Regex }
);

UPDATE:

I guess this is a known issue, thanks @max-toro

Community
  • 1
  • 1
mateuscb
  • 10,150
  • 3
  • 52
  • 76
  • Interesting, I had been looking for confirmation it was a known issue but hadn't found that thread. a url like `/n8n` works however (in the question Haacked seems to say `_id_` wouldn't work in addition to `__id`. – quentin-starin Jan 07 '12 at 20:08
  • The same day I posted this question I already implemented a workaround similar to what you posted because I needed to simply get it working. However, I didn't think of adding just `n`, etc, to the constraint - instead I made them all go to the same action and used a switch on the first character to dispatch from there. I will most likely award you the bounty for that, unless somehow a better answer is posted, which is unlikely since this covers it. – quentin-starin Jan 07 '12 at 20:09
  • @qes, it is quite an odd bug. Nice observation about the underline, i really can't quite understand how it is behaving. Thanks! – mateuscb Jan 07 '12 at 21:59
2

I haven't had much success getting routes to match with prefixing a constant to a route parameter like "const{param}". Have you tried using your prefix as a complete route segment, like "const/{param}"? Would that meet your requirements?

routes.MapRoute(

    "NonBrandedSlideshow",
    "n/{id}",
    MVC.Slideshow.NonBranded(), null,
    new { id = Settings.Base32Regex }
);

routes.MapRoute(
    "FullSlideshow",
    "f/{id}",
    MVC.Slideshow.Full(), null,
    new { id = Settings.Base32Regex }
);

...etc?

Update after comment #1

Understood. The only other thing I can think of to try would involve making the id param a catchall parameter. See if that works:

routes.MapRoute(

    "NonBrandedSlideshow",
    "n{*id}",
    MVC.Slideshow.NonBranded(), null,
    new { id = Settings.Base32Regex }
);

routes.MapRoute(
    "FullSlideshow",
    "f{*id}",
    MVC.Slideshow.Full(), null,
    new { id = Settings.Base32Regex }
);

However, these routes should be registered late, so that the controller doesn't end up routing any URL prefixed with n, f, etc. to these controller actions.

danludwig
  • 46,965
  • 25
  • 159
  • 237
  • Unfortunately, I am constrained on the requirement. This system has been in production for a good while and it is not feasible to change the url structure for these slideshows as they are already widely distributed. If it comes to it, I should be able implement a workaround by routing all of these to the same controller action by using one route (without the prefix) and just using logic in the action to check the string. However, I would much prefer to find a solution at the routing level. – quentin-starin Jan 03 '12 at 13:43
  • re: update - ArgumentException: "A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter." Doesn't look like that will work. – quentin-starin Jan 03 '12 at 14:10
  • Ok. I will leave the answer up as an example of what doesn't work to solve this. Good luck. – danludwig Jan 03 '12 at 14:14
1

have you tried matching them without the route contraint on the end?

EDIT: sorry for half an answer got interrupted

you need to try this for a route constraint

     routes.MapRoute(
            "NSlideshow",
            "{id}",
            new { controller = "SlideShow", action = "N", id = UrlParameter.Optional },
            new
            {
                id = @"^[n]{1}[0-9ABCDEFGHJKMNPQRSTVWXYZ]+"
            }
        );

     routes.MapRoute(
            "GSlideshow",
            "{id}",
            new { controller = "SlideShow", action = "G", id = UrlParameter.Optional },
            new
            {
                id = @"^[g]{1}[0-9ABCDEFGHJKMNPQRSTVWXYZ]+"
            }
        );

rinse and repeat for each case, in your action method strip the first character off the front of the id

Peter
  • 7,792
  • 9
  • 63
  • 94
1

This is a workaround. A weird one, I think...

As you routing rules are pretty simple, you can create a custom ControllerFactory and check the url. if the url matches your way, you can define which controller and action to use, and set the id paramenter based on the url. If not, just leave the regular behavior.

I know, it won't make use of the out-of-the-box routing, but as it doesn't work... The good news is as soon as it's fixed you can update the routes, remove the custom ControllerFactory and continue without the workaround.

Ivo
  • 8,172
  • 5
  • 27
  • 42