Is it possible to clear route values with RedirectToAction?
asp.net-mvc, routes
Solution
Thanks to @Slicksim I found that the answer is to remove the key from `RouteData.Values` rather than setting it to null:
public ActionResult Status(int id) {
if (id > 1000) {
TempData["ErrorMessage"] = "ID too high.";
RouteData.Values.Remove("id");
return RedirectToAction("Index");
}
// (code to display status)
}
Problem
I want to redirect to an action in the same controller, but lose the route values (in particular, the `id` value). This is turning out to be surprisingly difficult. I have routes configured like this: ``` context.MapRoute( "Monitoring_controllerIdSpecified", "Monitoring/{controller}/{id}/{action}", new { action = "Status" } ); context.MapRoute( "Monitoring_default", "Monitoring/{controller}/{action}", new { controller = "Events", action = "Index" } ); ``` ... and an action method inside `EventsController` something like this: ``` public ActionResult Status(int id) { if (id > 1000) { TempData["ErrorMessage"] = "ID too high."; return RedirectToAction("Index", new { id = (int?)null }); } // (code to display status) } ``` If I then access something like `/Monitoring/Events/1001`, the `RedirectToAction` is indeed invoked, but I get redirected to `/Monitoring?id=1001` instead of just `/Monitoring`. It seems to be matching the first route, `Monitoring_controllerIdSpecified`, even though that route has `id` as a mandatory route parameter and I told it to set `id` to null, and bizarrely turning `id` into a query string key. In other words, it is not properly clearing/removing the `id` route value. Setting `id` to an empty string in the `routeValues` object passed to `RedirectToAction` just gives the same effect as setting it to `null`. Why is it doing this and how can I convince it not to match the first route because `id` has been completely removed from the route values?