2009-07-28 73 views
5

如何從自定義ActionFilter訪問RedirectToAction?從ActionFilter RedirectToAction

public class ExceptionHandlingFilterAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     if (filterContext.Exception != null && !filterContext.ExceptionHandled) 
     { 
      filterContext.ExceptionHandled = true; 

      // HERE : RedirectToAction("ServiceNotFound","Error"); 

     } 
     base.OnActionExecuted(filterContext); 
    } 
} 

回答

1

你不是真的。您可以使用RedirectResult或RedirectToRouteResult。如果您正在根據身份驗證重定向,則應該考慮Controller是ActionFilter,因此您可能會從基本控制器類繼承此基本行爲。只需重寫基類中的OnActionExecuting方法即可。

8

試試這個:

filterContext.Result = new RedirectToRouteResult(
    new System.Web.Routing.RouteValueDictionary { 
     {"controller", "Error"}, {"action", "ServiceNotFound"} 
    } 
);