来自ActionFilter的RedirectToAction

Yoa*_*. B 5 asp.net-mvc

如何从自定义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);
    }   
}
Run Code Online (Sandbox Code Playgroud)

eu-*_*-ne 8

试试这个:

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


Mar*_*son 1

你真的不知道。您可以使用 RedirectResult 或 RedirectToRouteResult。如果您正在考虑基于身份验证的重定向,您应该考虑控制器是一个 ActionFilter,因此您可以从基本控制器类继承此基本行为。只需重写基类中的 OnActionExecuting 方法即可。