Chaddeus
Chaddeus

Reputation: 13366

In ASP.NET MVC, why doesn't this url match a route when using /id but ?id= works?

First, here's the route:

routes.MapRoute("PlaceRoutes", "{b}/Places/Show/{id}/{subaction}",
    new { b = "yokota-ab-japan", controller = "Places", action = "Show", id = UrlParameter.Optional, subaction = UrlParameter.Optional }
    );

This url: localhost/yokota-ab-japan/Places/Show/4b5bfc7ef964a520332029e3

does not match it,

this url: localhost/yokota-ab-japan/Places/Show?id=4b5bfc7ef964a520332029e3

does.

In fact somehow when /id is used, it simply routes back to the root homepage. When I run it in the debugger, it never even touches the Places/Show action, it simply routes back. However, if I use ?id= it routes fine.

I've never had this happen before... very confused. I tried to use Phil Haack's route debugger, but since it's not even touching the route and just loops back to the homepage, the debugger doesn't help.

Edit - Here's the full routes list

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute("PlaceRoutes", "{b}/Places/Show/{id}/{subaction}",
        new { b = "yokota-ab-japan", controller = "Places", action = "Show", id = UrlParameter.Optional, subaction = UrlParameter.Optional }
        );

    routes.MapRoute("BaseRoutes", "{b}/{controller}/{action}/{id}",
        new { b = UrlParameter.Optional, controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { controller = "Home|Member|Places|Search|Admin" }
        );

    routes.MapRoute(
        "NullBase",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { controller = "Home|Member|Places|Search|Admin|Auth" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Upvotes: 0

Views: 474

Answers (2)

Chaddeus
Chaddeus

Reputation: 13366

After much digging, it turns out it was only that one url. Somehow it had gotten a HTTP 301 code in the past, redirecting it back to the homepage... I guess browsers remember that. ;)

Upvotes: 0

motime
motime

Reputation: 564

if its asp.net mvc 3.0, its a known bug with two consecutive optional params

http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

Upvotes: 2

Related Questions