Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do ASP.NET MVC Routes work?

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.

like image 478
Pure.Krome Avatar asked Mar 13 '10 14:03

Pure.Krome


1 Answers

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. :)

like image 93
Arnis Lapsa Avatar answered Sep 28 '22 23:09

Arnis Lapsa