Response.Redirect not working inside an custom ActionFilter

老子叫甜甜 提交于 2020-01-01 19:24:05

问题


My code is the following

public class SessionCheckAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (/*condition*/)
        {
            filterContext.HttpContext.Response.Redirect("http://www.someurl.com",true);

        }
         base.OnActionExecuting(filterContext);
    }

}

Now, the question is WHY does the action that is has [SessionCheck] applied to it STILL executes. Any ideas? Thanks.


回答1:


Don't use Response.Redirect, rather replace the Result on the context with a RedirectResult. This will terminate processing in the filter chain and cause the redirect response to be sent immediately.

filterContext.Result = new RedirectResult( "http://www.someurl.com" );


来源:https://stackoverflow.com/questions/2765131/response-redirect-not-working-inside-an-custom-actionfilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!