If the redirected controller inherit from the same baseController
where we override the OnActionExecuting
method cause recursive loop. Suppose we redirect it to login action of account controller, then the login action will call OnActionExecuting
method and redirected to the same login action again and again
... So we should apply a check in OnActionExecuting
method to check weather the request is from the same controller if so then do not redirect it login action again. here is the code:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
try
{
// some condition ...
}
catch
{
if (filterContext.Controller.GetType() != typeof(AccountController))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
{ "controller", "Account" },
{ "action", "Login" }
});
}
}
}
new RedirectResult(url)
you could also usenew RedirectToAction(string action, string controller)
. This may have been added to MVC after you posted your answer. Your solution put me on the right track anyway. – Hobie