1

I study a MVC sample I have a controller Home and a action Index.

I have the following routing registration:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute("MyRoute", "{controller}/{action}");

    routes.MapRoute("MyOtherRoute", "App/{action}",
        new { controller = "Home" });
}

As I understood, if the requested route does not match (OR THE MATCH GIVES A NULL RESULT?!), the framework will continue to search in the rest of the routes...

By eg, if I will navigate to the /App/Index, I expect that I will be redirected to the Index method of Home Controller.

That is the case, but only in the "MyRoute" is defined after "MyOtherRoute" or "MyRoute" is missing at all. But as is presented before, I got a HTTP 404.

Why this?

0

2 Answers 2

2

As found in MSDN after some researches:

When a match occurs, no more routes are evaluated.

Apparently, there is no difference if that match gave or not the results...

1

MVC is validating the routes from the top and your first route is valid for /App/Index. It is not checked if the route leads to an existing controller / action.
You must add the route so that the more specific routes are at the beginning, then it will work as you need it.

3
  • it matches from the top, but the first match gives a null result(App controller does not exist), so it should go on searching?!
    – serge
    Sep 25, 2013 at 10:34
  • No, MVC will not go on searching, it will display HTTP 404 like you got. Just change the order and it should work.
    – Raidri
    Sep 25, 2013 at 10:50
  • As I mentioned in the question, changing the order works, so this is not a problem. As for me, it will be better if once no result is obtained from a match, the system continue to search matches...
    – serge
    Sep 25, 2013 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.