How do ASP.NET MVC Routes work?

2019-04-07 07:41发布

问题:

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:

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



回答2:

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.



回答3:

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.