serge
serge

Reputation: 15239

MVC Routing: route precedence not working as expected

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?

Upvotes: 1

Views: 779

Answers (2)

serge
serge

Reputation: 15239

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

Upvotes: 2

Raidri
Raidri

Reputation: 17550

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.

Upvotes: 1

Related Questions