I have a simple ActionResult in my controller:
public ActionResult Index(int productId)
{
return View();
}
If I breakpoint it, I can confirm the productId is being passed through and is not null. However, if I examine my RouteData:
(int)RouteData.Values["productId"]
There’s nothing there. There’s a key for controller and a key for the action, but nothing for the parameter? What’s going on?
Did you declare the
productIdas token somewhere in your routes?If you haven’t don’t expect to find it in the
RouteDatacollection. This collection contains only tokens that were declared in your routes.The default route uses
id(the one generated by Visual Studio wizard), so you could rename your action parameter:which would totally make sense in a
ProductsControllerfor example. And now you will find it inRouteData.Values["id"].