views:

205

answers:

3

Hi folks,

I have the following route's defined:

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

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" }
    );

    // Added custom route here!
    routes.MapRoute(
        "CatchAll", 
        "{*catchall}," 
        new { controller = "Error", action = "NotFound" }
    );
}

nothing new - that's the default ASP.NET MVC1 RegisterRoutes method, with one custom route added.

Now, if I goto the following url, i get a 404...

http://whatever/Home/MissingActionMethod

So there's no ActionMethod called MissingActionMethod in the HomeController. So, does this mean, if i goto the 1st route defined, above .. and fail to find an action .. do I then come back and try the second route? rinse-repeat?

Or once i match a route, i then try and execute that route .. and if i fail (ie, find the action is missing) .. then .. bad luck? boomski?

cheers!

EDIT/UPDATE:

Thanks heaps for the replies, but they are not reading my question properly :( I know 1) order of routes are important b) haack's route debugger

but my question is not about that. I'm asking that .. if the first route is 'handled' .. but fails .. does it then go down the list to the next one?

So, in my example above. The first route called 'Default' is matched against the url/resource requested ... but when the framework tries to find an action, which is missing .. it 404's.

So .. does that mean the framework first matches the "default" route .. tries it .. fails .. goes BACK to the route list .. tries to find the next route that matches .. and finally fails so it then gives up?

Or it only finds the first and only the first route it matches .. and if it fails to find the controller and/or action .. then it just gives up there and then? (This is what i suspect). And if so .. how does it then figure out how to 404?

Update #2:

Phil Haack actually talks about my question, a bit ... but doesn't answer the part I was curious about -> how and where it determines a 404 resource not found.

+1  A: 

I don't think it will check the second route, because the first one specified is the default. I think if you switch them, it would check CatchAll, see that it doesn't match the route specified in the URL, and then fall back to the default, since you're only providing a controller name, not a route. I think if you wanted CatchAll to do anything at all, you'd have to hit http://whatever/CatchAll/Error/MissingActionMethod, and it would have to come before the default.

See this for more in-depth information.

Mike Pateras
In short - order you specify routes is **important**!
Arnis L.
+2  A: 

Routes != Actions.

It goes like this - on incoming request, routing module searches for first route in route table that matches and then tries to call appropriate action.

If action is not found, request fails and returns 404 (it does NOT try to look for next route).


But it should be possible to extend framework in order to achieve this. My first guess - You could write your own RouteHandler.

  1. RouteHandler
    Not really specific to ASP.NET MVC, the RouteHandler is the component that decide what to do after the route has been selected. Obviously if you change the RouteHandler you end up handling the request without ASP.NET MVC, but this can be useful if you want to handle a route directly with some specific HttpHanlders or even with a classic WebForm.

Anyway - I wouldn't recommend it though. It's better to keep routing dumb.


After some quick googling - I'm not so optimistic about this anymore. :)

Arnis L.
Ok. so it returns 404. does it fire off an HttpException? if so, then what about any custom exception handling? it is possible to GRAB this 404 error and send it to your own page? etc?
Pure.Krome
@Pure.Krome there might a be way to customize routing/mvc to work as you want. Just haven't got time to take a closer look.
Arnis L.
A: 

You should try using the Phil Haack's route debugger from http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx, to see what route is matching and why.

eKek0