ActionFilterAttribute not redirecting
Asked Answered
B

2

8

I have a problem with an ActionFilterAttriute that is not redirecting correctly. I'm not familiar with the complete codebase, but I have seen enough to not understand what is going on.

To simplyfy the code, I have removed the irrelevant parts:

public class ResolveApplicationRedirectAttribute : ActionFilterAttribute
{
    //some variables
    private ActionExecutingContext _filterContext;

    protected string ApplicationRedirectUrl
    {
        get { return ConfigurationManager.AppSettings["ApplicationRedirectUrl"]; }
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _filterContext = filterContext;

        //a lot of logic that decide if we should redirect or not

        //I added this after the other logic to make it always redirect regardless of what happens inside the logic above
        _filterContext.Result = new RedirectResult(ApplicationRedirectResult);
    }
}

[ResolveApplicationRedirect]
public ActionResult Index(CrmQueryStringParameters crmParameters){
  //some logic
}

This usually works, but when the application gets hit by a few requests in a short period of time, the Index method ends up being called, and blows up because the View is missing some data (we know it's missing data. That is why we want to redirect).

But Now when I have added the _filterContext.Result = new RedirectResult(ApplicationRedirectResult) as the last line of the OnActionExecuting method, how is it possible that it still calls my Action method?

Are there any knows bugs/corner cases/anything else that can cause MVC to disregard the RedirectResult I have put into the filterContext and fire the action method anyway?

Anything special that can be inside the logic of OnActionExecuting that can cause problems even if I set the filterContext.Result as the last line. Any exceptions inside the attribute should just blow it up, and not skip the attribute and call the Action method.

Any help to point me in the right direction would be appreciated here.

Boltonia answered 23/8, 2013 at 12:11 Comment(1)
If you look at the source code for MVC you will see a method InvokeActionMethodFilter that executes OnActionExecuting on your filter passing the ActionExecutingContext. If it returns with a non-null Result value it returns an ActionExecutedContext skipping invoking your action. So, in a way what you are seeing shouldn't be possible. So, the only problem could be a null value for Result. If you comment all your logic and keep only the last line, is your redirect working then?Ingrain
B
0

I found the problem at last. It was related to using the _filterContext class variable.

The problem is that MVC does not create a new instance of the filter for each request, causing several requests to share the same instance of the attribute.

For more details, see the list of breaking changes in the MVC3 release notes.

Boltonia answered 26/8, 2013 at 8:30 Comment(0)
S
0

+1 for Wouter's result. Given the code you've shown, it shouldn't be possible to achieve the results you describe. Perhaps the // magic logic here is returning a null _filterContext.Result?

Sonja answered 26/8, 2013 at 4:46 Comment(0)
B
0

I found the problem at last. It was related to using the _filterContext class variable.

The problem is that MVC does not create a new instance of the filter for each request, causing several requests to share the same instance of the attribute.

For more details, see the list of breaking changes in the MVC3 release notes.

Boltonia answered 26/8, 2013 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.