RedirectToAction
受到保护,我们只能在行动中使用它.但是,如果我想在过滤器中重定向?
public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
filterContext.Result = (filterContext.Controller as Controller)
.RedirectToAction("Index", "Home");
}
}
Run Code Online (Sandbox Code Playgroud) 我编写了一个动作过滤器,用于检测新会话并尝试将用户重定向到一个页面,通知他们已经发生这种情况.唯一的问题是我无法弄清楚如何将其重定向到动作过滤器中的控制器/动作组合.我只能弄清楚如何重定向到指定的网址.有没有直接的方法重定向到mvc2中的动作过滤器中的控制器/动作组合?
我正在使用ASP.NET Core.我的一个控制器调用服务,抛出各种异常.我想在异常过滤器(而不是中间件)中处理它们.
public class MyHandlerAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext c)
{
if (c.Exception is FooException) {
// redirect with arguments to here
}
else if (c.Exception is FooException) {
// redirect with arguments to there
}
else {
// redirect to main error action without arguments, as 500
}
base.OnException(c);
}
}
Run Code Online (Sandbox Code Playgroud)
与动作过滤器不同,异常过滤器不会让我访问Controller
,所以我不能这样做c.Result = controller.RedirectTo...()
.
那么如何重定向到我的错误操作?