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 }); }
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?
Jez
source share