Why won't this route match

纵饮孤独 提交于 2019-12-04 07:14:19
mateuscb

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

Max Toro

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.

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!