I got a custom routes created for 2 different actions in same controller:
routes.MapRoute(
name: "editEquivPack",
url: "equivpacks/{id}/{ecommerceid}",
defaults: new { controller = "EquivPacks", action = "Edit" }
);
routes.MapRoute(
name: "addEquivPack",
url: "equivpacks/add/{ecommerceid}",
defaults: new { controller = "EquivPacks", action = "Add" }
);
In a
URL.RouteURL("addEquivPack", ecommerceid = Model.EcommerceID)
it generates a correct URL:
http://localhost:53365/EquivPacks/Add/1
But when i try to navigate there, it sends me a error message:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32, Int32)' in 'XXXXXXX.Controllers.EquivPacksController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
It seems that it executes Edit action and not Add action that is the action configured in route map.
How can i fix it?
The order of route definitions is important and the first match wins. Your first route definition (
editEquivPack) means match a url containing 3 segments, where the first segment is "equivpacks".Your url of
../EquivPacks/Add/1matches that, so it then calls theEdit()method and passes a value of"Add"to yourint idparameter in that method (which cannot be bound to anint, hence the error).You need to change the order of your routes so that the
addEquivPackroute is before theeditEquivPackroute.