Redirecting to specified controller and action in asp.net mvc action filter

后端 未结 3 590
后悔当初
后悔当初 2020-11-29 21:41

I have written an action filter which detects a new session and attempts to redirect the user to a page informing them that this has happened. The only problem is I can not

3条回答
  •  旧时难觅i
    2020-11-29 22:16

    Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It's a bit cleaner and better for testing.

    Like this:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if(something)
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary {{ "Controller", "YourController" },
                                          { "Action", "YourAction" } });
        }
    
        base.OnActionExecuting(filterContext);
    }
    

提交回复
热议问题