ASP.NET MVC 2 RC2 Routing - How to clear low-level values when using ActionLink to refer to a higher

Posted by Gary McGill on Stack Overflow See other posts from Stack Overflow or by Gary McGill
Published on 2010-03-23T15:10:39Z Indexed on 2010/03/23 15:13 UTC
Read the original article Hit count: 653

[NOTE: I'm using ASP.NET MVC2 RC2.]

I have URLs like this:

/customer/123/order/456/item/index

/customer/123/order/456/item/789/edit

My routing table lists the most-specific routes first, so I've got:

// customer/123/order/456/item/789/edit
routes.MapRoute(
    "item", // Route name
    "customer/{customerId}/order/{orderId}/item/{itemId}/{action}", // URL with parameters
    new { controller = "Items", action = "Details" }, // Parameter defaults
    new { customerId = @"\d+", orderId = @"\d+", itemId = @"\d+" } // Constraints
);

// customer/123/order/456/item/index
routes.MapRoute(
    "items", // Route name
    "customer/{customerId}/order/{orderId}/item/{action}", // URL with parameters
    new { controller = "Items", action = "Index" }, // Parameter defaults
    new { customerId = @"\d+", orderId = @"\d+" } // Constraints
);

When I'm in the "Edit" page, I want a link back up to the "Index" page. So, I use:

ActionLink("Back to Index", "index")

However, because there's an ambient order ID, this results in the URL:

/Customer/123/Order/456/Item/789/Index

...whereas I want it to "forget" the order ID and just use:

/Customer/123/Order/456/Item/Index

I've tried overriding the order ID like so:

ActionLink("Back to Index", "index", new { orderId=string.empty })

...but that doesn't work.

How can I persuade ActionLink to "forget" the order ID?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2