views:

58

answers:

1

Is there any way to force a Route to be executed, only if all tokens are present in the URL string?

Consider this Route:

RouteTable.Routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new
    {
        controller = "Test",
        action = "Index",
        id = 0
    }
);

This Route execute on /Home/Index/1 and /Home/Index. But I only want it to get executed if all tokens are present in the URL string: controller, action and id.

+3  A: 

Simple: take out the default ID value and it will require all three to be set.

Wyatt Barnett
Worked like a charm! Come to think of it; why would you want to define defaults at all? Whats the use-case for doing so?
roosteronacid
If you write www.yourpage.com, you actually want to go to www.yourpage.com/Home/Index, so you declare defaults.
LukLed
@LukLed: In that case, couldn't you add: _RouteTable.Routes.MapRoute("Default", "", new { controller = "Weblog", action = "Entries" });_?
roosteronacid
That doesn't make sense. Do you want to write two routes instead of one, so one would have no default parameters and other one had only default parameters? And still you end up using defauls:)
LukLed
Well in this case, defining defaults on that specific route breaks my other related routes. So yeah :)
roosteronacid