0

see my url localhost:55831/Customers/Edit/1/ALFKI which i generate using @Html.RouteLink

@Html.RouteLink("Edit", "PageWithId",
new
{
    controller = "Customers",
    action = "Edit",
    id = item.CustomerID,
    page = ViewBag.CurrentPage
})

here is my routing code for PageWithId i tried two routing one after one but none work

    routes.MapRoute(
        name: "PageWithId",
        url: "{controller}/{action}/{page}/{id}"
    );

AND

routes.MapRoute(
    name: "CustomerEditWithId",
    url: "Customers/Edit/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

my all routing code

routes.MapRoute(
name: "PageWithSort",
url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }
);

routes.MapRoute(
name: "PageWithId",
url: "{controller}/{action}/{page}/{id}"
);


routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

please some one help because when i am clicking ob this link localhost:55831/Customers/Edit/1/ALFKI then customer id ALFKI is not passing to edit action. i debug the edit action code and found id is getting null always. i tried few routing for edit action but no luck still.

1
  • For organisational when you have this kind of routing it is advised to use areas. They make such things very easy
    – Jack M
    Commented Feb 6, 2018 at 16:06

1 Answer 1

1

The order of routes matters. Routes are checked in order and the first matching route wins.

Because your first (PageWithSort) route matches any route with between 1 and 5 segments, then /Customers/Edit/1/ALFKI, which contains 4 segments matches, and no further routes are checked. And since ALFKI is being passed to the 4th segment named SortColumn, then it will only be bound to a parameter named SortColumn in your Edit() method (not to something named id)

As it stands, your PageWithId and Default routes are pointless since they can never be hit.

In additional, only the last parameter in a route definition can be marked with UrlParameter.Optional.

You need to create specific routes, and locate them in the correct order. Change your route definitions to (note I have omitted the PageWithSort route because its not clear what you want to do with that and it contains errors anyway)

routes.MapRoute(
    name: "CustomerEditWithId",
    url: "Customers/Edit/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Now any url that begins with /Customers/Edit and contains 2 additional segments will match the CustomerEditWithId route, and the 3rd segment will be bound to a parameter named page and the 4th segment will be bound to a parameter named id. Therefore your method in CustomersController should be

public ActionResult Edit(int page, string id)
2
  • i will update my post with current routing code Monday. i would love to get more guidance to handle my situation. Commented Feb 10, 2018 at 19:58
  • I'm not sure what your PageWithSort route is for (that is not explained in your question), but if you place that route after CustomerEditWithId and change it to defaults: new { controller = "Home", action = "Index" } - i.e. remove all the UrlParameter.Optional then any route that contains exactly 5 segments would now match it.
    – user3559349
    Commented Feb 11, 2018 at 0:37

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.