0

I had an ASP.Net MVC application with routing configuration rules including this one:

routes.MapRoute(
        "rule1",
    "{controller}/{action}/{cid}/{language}/{itemID}/{uid}",
    new { controller = "Home", action = "action1" }
    , new[] { "MVCApp.Controllers" }
);

I just added new action with same number of parameter and my routing configuration changed like this (rule2 added):

routes.MapRoute(
        "rule2",
    "{controller}/{action}/{cid}/{language}/{phoneNumber}/{uid}",
    new { controller = "Home", action = "action2" }
    , new[] { "MVCApp.Controllers" }
);
routes.MapRoute(
        "rule1",
    "{controller}/{action}/{cid}/{language}/{itemID}/{uid}",
    new { controller = "Home", action = "action1" }
    , new[] { "MVCApp.Controllers" }
);

Now when I call http://localhost:51650/Home/action2/1/en/1/1 it does not route to action1 and throws exceptionThe parameters dictionary contains a null entry for parameter 'itemID' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.JsonResult action1(System.String, Int32, Int64, Int64).

2
  • Invert the order. The order in which the routes are added affects how the routes works. add first rule1 and then rule2 Commented Feb 22, 2017 at 17:00
  • @NicoRiff I've just updated exception details. Is there any way that information does not route to rule2 and routing skip it and take rule1 in action (for url http://localhost:51650/Home/action2/1/en/1/1)? Commented Feb 22, 2017 at 17:06

1 Answer 1

2

You have to put first your rule1 and then your rule2 declaration. The order in which the routes are declarated is important to the api and as soon as a request match a route, then the api will stop searching for routes and go to the specified controller.

This is from the Pro ASP.NET Web API book:

Depending on your needs, you might want to have multiple Web API routes in your application. This is a totally acceptable option in ASP.NET Web API, but there are a few important things to be aware of. If you have multiple routes, the registration order of the routes matters. When a request comes to the routing level, the route collection is scanned to find a match. As soon as a match is found, the search stops, and the remaining routes get ignored. The first route registered will be looked at first, and so on.

So as your two routes have the same parameter quantity, it exits on the first route that match the request parameter quantity. To solve that, you might add constraints to your routes so you can ensure what value goes to what route, or hardcode your route to action1 in rule1 and action2 to rule2 as @Pavel as stated.

For hardcoding of action to route rule1 always to action1 and rule2 always to actoin2, use: {controller}/action1/{cid}/{language}/{itemID}/{uid} for action1 and do the same for action2 and rule2

Sign up to request clarification or add additional context in comments.

6 Comments

how can i hardcode action in rule2 or rule1? Since changing the order will lead to same error on the other action.
when someone input action1 is supposed to go to rule1 and action2 to rule2?
I should note phoneNumber is string and itemId is int and when i reverse the order of rules, the phoneNumber will be null when information is routed to action2 .
when someone input action1 is supposed to go to rule1 and action2 to rule2 --> Yes, that's right
use: "{controller}/action1/{cid}/{language}/{itemID}/{uid}" for action1 and do the same for action2 and rule2
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.