0

I'm using donut output caching on a certain action, which uses this action filter:

public sealed class DoStuffAttribute : ActionFilterAttribute {


  private bool HasRun = false;


  public override void OnActionExecuting(ActionExecutingContext context) {
    HasRun = true;
  }
  public override void OnActionExecuted(ActionExecutingContext context) {
    HasRun = true;
  }


  public override void OnResultExecuting(ResultExecutingContext context) {
    if (HasRun) doSomething();
  }
  public override void OnResultExecuted(ResultExecutedContext context) {
    if (HasRun) doSomething();
  }


}

For the first request, HasRun is set, and then the action's results are cached.

For following requests, OnActionExecuting and OnActionExecuted are not run, as the action is cached. BUT, in the OnResultExecuting and OnResultExecuted methods, which do run, the HasRun flag is true!

Does OutputCache cache not only the result, but also the state of action filters? If not, what is happening here?

h bob
  • 3,610
  • 3
  • 35
  • 51
  • 1
    Your code works fine for me - `HasRun` is set to `true` and `doSomething()` is executed because of it. –  Sep 26 '15 at 09:12
  • @StephenMuecke Sorry, I didn't pose the question properly. I've edited it so it corresponds to the behavior I'm seeing. – h bob Sep 26 '15 at 09:24
  • 1
    If the action is cached, it would make sense to keep also any information about it, having an instance of it if I may call it so, including its filter attributes, so the first two methods, OnActionExecuting and OnActionExecuted would be executed only once, and following calls won't need to execute the action again, since is cached, but only return the result, this is why the other two methods do get executed, the OnResultExecuting and OnResultExecuted. I haven't used donut caching, so I might be wrong, but my logic would say that this is the right behavior. – lex87 Sep 26 '15 at 09:48
  • [I found this](http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3#109) which goes some way to explain the ordering, but not why the property is cached. – h bob Sep 26 '15 at 09:48

0 Answers0