Archive for July, 2008

The behaviour of RedirectToAction(string action) changed in the ASP.NET MVC CTP 3

No Comments »

In the ASP.NET MVC CTP 2 the RedirectToAction(string action)-method will redirect to the action named on the same controller. However, this doesn’t happen in the new CTP 3. In this CTP the controller will not be set and the user will be redirected to the first route in the route table which matches the action name. Thus, you should change the call

RedirectToAction("Index")

in an action in MyController to

return RedirectToAction("Index", "MyController")

when migrating your code from CTP 2 to CTP 3. You could also create your own RedirectToCurrentControllerAction method on your controller, setting the controller explicitly:


public RedirectToRouteResult RedirectToCurrentController(string action)
{
RedirectToRouteResult red = RedirectToAction(action);
red.Values.Add("controller", this.ControllerContext.RouteData.Values["controller"]);
return red;
}

and in your action use

return RedirectToCurrentController("Index");