Route with Two optional parameters in MVC3 not working
Solution 1:
You're missing the parameters in your route config. In order to make this work with different parameters optional (as in Phil Haack's post), you need to define multiple routes
routes.MapRoute("UserDetail-WithStatus",
"UserDetail/{id}/{inSaveAction}/{status}",
new
{
controller = "Admin",
action = "UserDetail",
// nothing optional
}
);
routes.MapRoute("UserDetail-WithoutStatus",
"UserDetail/{id}/{inSaveAction}",
new
{
controller = "Admin",
action = "UserDetail",
// nothing optional
}
);
routes.MapRoute("UserDetail-WithoutSaveAction",
"UserDetail/{id}",
new
{
controller = "Admin",
action = "UserDetail",
id = UrlParameter.Optional
}
);
And then create links with:
@Html.ActionLink("Link", "Index", "Admin", new { id = 1, inSaveAction = true, success = "success" }, null)
You'll also need to set the optional parameters as nullable, otherwise you'll get exceptions if id or inSaveAction are missing.
public ActionResult UserDetail(int? id, bool? inSaveAction, string status)
{
}