2

An existing RedirectToAction signature is

RedirectToAction(string action, RouteValueDictionary routeValues);

I wish to make.

RedirectToAction(RouteValueDictionary routeValues);

So I created the following

public static class MvcControllerExtension
{
    public static RedirectToRouteResult RedirectToAction
        (this Controller controller, RouteValueDictionary routeValues)
    {
        return controller.RedirectToAction
                    (routeValues["Action"].ToString(), routeValues);
    }
}

However, the IDE for that code is showing a Recursive Call because it can only see itself.

It does not see this signature. enter image description here

I have included using System.Web.Mvc; in the extension class.

How can I fix this? thanks.

Additional:

Here is the extention source code. Note the recursive symbol.

(Sorry, SO is having issues uploading images to imgur.com. Will retry soon).

Valamas
  • 24,169
  • 25
  • 107
  • 177
  • This appears to be the same problem as this post: http://stackoverflow.com/questions/2118064/extension-methods-overloading-in-c-does-it-work – Paul Zaczkowski Nov 02 '11 at 04:33

2 Answers2

4

Why do an extension? RedirectToAction is a helper method in the Controller class, so why not create yours as a protected method in your base controller class?

public abstract class MyControllerBase : Controller
{
   protected RedirectToRouteResult RedirectToAction(RouteValueDictionary routeValues)
   {
      return RedirectToAction(routeValues["Action"].ToString(), routeValues);
   }
}
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • 1
    @Valamas It might also be helpful to note, the reason you were having an issue is because `Controller.RedirectToAction` is marked `protected`. It is only intended to be used from within a `Controller`, so that's why your extension method cannot "see" it, and can only "see" itself. Change the name of your extension method, and the error will be much easier to spot. – Scott Rippey Nov 02 '11 at 07:03
  • I see that now. I also got carried away with extension madness on this one. thank you for adding that – Valamas Nov 02 '11 at 07:31
0

What namespace is MvcControllerExtension in - it must be in the same namespace or have the import for it.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • The problem is before the usage of the code. The error I am having is at the source of the extension. I have updated my post with the error. – Valamas Nov 02 '11 at 04:20