2
protected internal RedirectToRouteResult RedirectToAction(
    string actionName,
    string controllerName);

means I cannot do

public static ActionResult RedirectToErrorAction(this Controller controller)
{
    // redirect to custom error page
    return controller.RedirectToAction("error", "support");
}

Any ideas?

3
  • any idea to a workaround Commented Mar 21, 2009 at 1:33
  • From what youre saying your action name is "error" and it lies in your controller that is named supportcontroller, but why are you naming a controller action an mvc method name Commented Mar 21, 2009 at 1:48
  • RedirectToErrorAction is an extension method added to Controller so I may use it across many controllers. Commented Mar 21, 2009 at 2:16

1 Answer 1

1

You might want to consider returning a custom, shared error view instead, but if you really need to do this, you might want to consider using reflection to invoke the internal method. The former could be implemented in a base controller that becomes the foundation for all your controllers.

First example:

public class BaseController : Controller
{ 
    private ActionResult CreateErrorResult( string message )
    {
         ViewData["message"] = message;
         ...
         return new View( "CustomError" );
    }
}

Second example (if getting the internal attribute via reflection works):

public static ActionResult RedirectToErrorAction(this Controller controller)
{
    MethodInfo info = typeof(Controller).GetMethod( "RedirectToAction",
                            BindingFlags.NonPublic|BindingFlags.Instance,
                            null,
                            new Type[] { typeof(string), typeof(string) },
                            null );
    return info.Invoke( controller, new object[] { "error", "support" } )
               as ActionResult;
}
Sign up to request clarification or add additional context in comments.

2 Comments

GetMethod will not work on protected methods and the Invoke needs to be cast to ActionResult.
Sorry -- you might be able to get it using BindingFlags.NonPublic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.