views:

63

answers:

2

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.

+4  A: 

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" );
tvanfosson
A: 

Thanks! I thought I did this on my laptop at home and it didn't work. However, I did this on a project at work, and it worked great.

mitch
BTW, I was not using my new account when posting this question. How do I link this new account to that question in order to mark it as the answer?
mitch