Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

+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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top