1

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?

0

1 Answer 1

1

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/1 matches that, so it then calls the Edit() method and passes a value of "Add" to your int id parameter in that method (which cannot be bound to an int, hence the error).

You need to change the order of your routes so that the addEquivPack route is before the editEquivPack route.

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

Comments

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.