views:

157

answers:

2

I've looked at the routing on StackOverflow and I've got a very noobie question, but something I'd like clarification none the less.

I'm looking specifically at the Users controller

http://stackoverflow.com/Users
http://stackoverflow.com/Users/Login
http://stackoverflow.com/Users/124069/rockinthesixstring

What I'm noticing is that there is a "Users" controller probably with a default "Index" action, and a "Login" action. The problem I am facing is that the login action can be ignored and a "UrlParameter.Optional [ID]" can also be used.

How exactly does this look in the RegisterRoutes collection? Or am I missing something totally obvious?

EDIT: Here's the route I have currently.. but it's definitely far from right.

    routes.MapRoute( _
        "Default", _
        "{controller}/{id}/{slug}", _
        New With {.controller = "Events", .action = "Index", .id = UrlParameter.Optional, .slug = UrlParameter.Optional} _
    )
+2  A: 

Probably just uses a specific route to handle it, also using a regex to specify the format of the ID (so it doesn't get confused with other routes that would contain action names in that position).

// one route for details
routes.MapRoute("UserProfile",
     "Users/{id}/{slug}",
     new { controller = "Users", action = "Details", slug = string.Empty },
     new { id = @"\d+" }
);
// one route for everything else
routes.MapRoute("Default",
     "{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
John Nelson
I agree this straight forward approach will work but also I suspect author's approach a bit more sophisticated: routes are stored in external files and loaded in runtime while java script routing helpers generated per first request :) Wonder about best practices here
Andrew Florko
Regardless of how the routes get loaded in, you have to have some specific way of describing how the route maps to the controller.action with the proper parameters. Right?
John Nelson
Yes. It would be very nice if authors will come down and answer :)
Andrew Florko
Beautiful.. this worked (I had to convert it to VB). Thanks.
rockinthesixstring
@rockinthesixstring no charge :-)
John Nelson
A: 

Without a SO developer giving a definite answer, reverse engineering could yield many possible combinations and permutations. Here's one that I think would fit too:

routes.MapRoute(
    "UserProfile",
    "Users/{id}/{slug}",
        new { controller = "Users", action = "Profile" }
);

routes.MapRoute(
    "UserLogin",
    "Users/Login",
    new { controller = "Users", action = "Login" }
);

routes.MapRoute(
    "DefaultUser",
    "Users",
    new { controller = "Users", action = "Index" }
);
Khnle