eWorld.UI - Matt Hawley

Ramblings of Matt

RedirectToAction Nasty Bug in ASP.NET MVC Preview 3

June 2, 2008 22:04 by matthaw

We were converting the CodePlex application over to ASP.NET MVC Preview 3 today and found a nasty bug with RedirectToAction. In reality, the bug isn't so much around RedirectToAction, but a change they made internally to Routing. However, this seems to only happen in certain routing scenarios. Take the following routes:

   1:  routes.MapRoute("Login", "site/home/{action}",
   2:     new { controller = "session", action = "login" });
   3:  routes.MapRoute("User Info", "site/user/{action}",
   4:     new { controller = "user", action = "show" });

The problem crops up within your UserController actions when attempting to redirect to other UserController actions. For instance

   1:  public class UserController : Controller {
   2:     public ActionResult Show() { ... }
   3:     public ActionResult Create() {
   4:        return RedirectToAction("Show");
   5:     }
   6:  }

when line 4 is executed above, it attempts to redirect you to "~/site/home/foo". The reason is the change that was made is now trying to remove all the ambiguities by "assuming" things on the fly. Since the actionName overload of RedirectToAction doesn't take and doesn't supply the controller it cannot find the appropriate route. After a bit of convincing, the bug has been acknowledge but I make no guarantees if and when it'll be fixed in a future build - but in the mean time, you're stuck with

  1. Fixing the code yourself from the CodePlex source drop. This simply involves supplying the current executing controller's name in the route value dictionary.
  2. Use the RedirectToAction overload that takes both actionName and controllerName.
  3. Use my lambda expression based RedirectToAction which correctly sends both actionName and controllerName.

And, before anyone asks - why are you creating such complicated routing? Well, simple - simple routing equals a very simple application. Complex routing equals a pre-existing and well defined "RESTful based" urls that mean something when a user reads it.

kick it on DotNetKicks.com


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

June 3. 2008 03:07

Pingback from weblogs.asp.net

RedirectToAction Nasty Bug in ASP.NET MVC Preview 3 - eWorld.UI - Matt Hawley

weblogs.asp.net

June 3. 2008 04:13

I ran into the same problem - as this affects the URL generator too, a number of my forms were posting to the wrong url.

Jeremy Skinner

June 3. 2008 04:46

Forgot to mention the workaround that I used Smile

You can create a custom route that will process the RouteValueDictionary and then add it as the first route in your routetable:

RouteTable.Routes.Add(new RouteValuePreParser());

public class RouteValuePreParser : RouteBase {
public override RouteData GetRouteData(HttpContextBase httpContext) {
return null;
}

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
if(!values.ContainsKey("controller") && requestContext.RouteData.Values.ContainsKey("controller")) {
values.Add("controller", requestContext.RouteData.Values["controller"]);
}
return null;
}
}

Jeremy Skinner

June 3. 2008 09:31

I worked around it a different way too - all of my controllers inherit off of a BaseController and overrides the RedirectToAction method injecting in the controller name:

protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, System.Web.Routing.RouteValueDictionary values)
{
if(String.IsNullOrEmpty(controllerName))
controllerName = RouteData.Values["controller"].ToString();

return base.RedirectToAction(actionName, controllerName, values);
}

Max Pool

July 15. 2008 19:19

The way I fixed was calling

return this.RedirectToRoute("TripFinder", new { controller = "TripFinder", action = "Index", id = id });

instead of the RederictToAction... plus u don't have to manipulate any code there fore making the upgrade straight forward once the new Preview comes out.

Samiq

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 28. 2008 04:43



Copyright © 2000 - 2008 , Excentrics World