How to redirect from OnActionExecuting in Base Controller?

前端 未结 4 987
刺人心
刺人心 2020-11-28 01:51

I have tried two ways: Response.Redirect() which does nothing, as well as calling a new method inside of the Base Controller that returns an ActionResult and have it return

4条回答
  •  悲&欢浪女
    2020-11-28 02:19

    Create a separate class,

        public class RedirectingAction : ActionFilterAttribute
        {
          public override void OnActionExecuting(ActionExecutingContext context)
          {
            base.OnActionExecuting(context);
    
            if (CheckUrCondition)
            {
                context.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Home",
                    action = "Index"
                }));
            }
          }
       }
    

    Then, When you create a controller, call this annotation as

    [RedirectingAction]
    public class TestController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
    

提交回复
热议问题