Controller.OnAction Do not fire after first visit to action

In my MVC 4 web application, I have a requirement to ask users to choose a payment method after they have used the application for a certain number of days.

I implemented this by creating a base controller class and making all my controllers inherit from this. The base class has the following code:

public class MyBaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (WebSecurity.IsAuthenticated)
        {
            var pay = PaymentDetails.LoadByMember(WebSecurity.CurrentUserId);
            if (pay.ForceToChooseMandate)
            {
                filterContext.Result = RedirectToAction("Choose", "Payment");
            }
        }
        base.OnActionExecuting(filterContext);
    }
}

      

There Choose

are links in the action to set up your payment details or postpone a selection for a while. If any of these links are visited, it pay.ForceToChooseMandate

returns false

on subsequent calls, allowing the application to resume normal operation for the user.

The first time the activity is visited, it OnActionExecuting

fires and the redirect works as fine. But if the user visits the activity a second time, it OnActionExecuting

never fires, and they are redirected to the activity again Choose

, although pay.ForceToChooseMandate

it would be appreciated for him this time false

.

However, if they attend a previously invisible activity, it OnActionExecuting

fires as expected.

How can I stop this and it OnActionExecuting

always lights up so it can always reflect the current state of the user?

+3


source to share


1 answer


The problem is that the result of your first execution is cached .

To achieve what you want, you need to disable caching for Action in Questions. You can do this by decorating those Actions withOutputCacheAttribute

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]

      



You can also enable the cache, but specify VaryByParam

to update it when needed.

Alternatively, you can remove the cache file from another activity with

HttpResponse.RemoveOutputCacheItem("/Home/About");

      

+2


source







All Articles