Is it possible to clear route values ​​using RedirectToAction?

I want to redirect to an action in the same controller, but lose the route value (in particular, the id value). This is surprisingly complicated. I have routes configured as follows:

 context.MapRoute( "Monitoring_controllerIdSpecified", "Monitoring/{controller}/{id}/{action}", new { action = "Status" } ); context.MapRoute( "Monitoring_default", "Monitoring/{controller}/{action}", new { controller = "Events", action = "Index" } ); 

... and the 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 , RedirectToAction actually called, but I am redirecting to /Monitoring?id=1001 instead of just /Monitoring . It seems to correspond to the first Monitoring_controllerIdSpecified route, although this route has id as a required route parameter, and I told it to set id to null and bizarrely turn id into a query string key. In other words, it is incorrect to clear / delete the id route value. Setting id to an empty string in the routeValues object passed to RedirectToAction the same effect as setting null .

Why is this done and how can I convince it to not match the first route, because id completely removed from the route values?

+7
source share
1 answer

Thanks to @Slicksim, I found that the answer is to remove the key from RouteData.Values , and not set 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) } 
+10
source

All Articles