tags:

views:

313

answers:

1
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?

+1  A: 

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;
}
tvanfosson
GetMethod will not work on protected methods and the Invoke needs to be cast to ActionResult.
blu
Sorry -- you might be able to get it using BindingFlags.NonPublic.
tvanfosson