1

I have a controller which calls a method in the controller called GetTypes()

[OutputCache(Duration = 6000)]
public List<SelectListItem> GetTypes()
{
   return Datetime.Now();
}

Why does this method not cache if I call it from inside of my controller from another action method? It only seems to cache when I call it directly using the URL or @Render..

"localhost/home/GetTypes"

1 Answer 1

3

Yes, that is the way it works, and the way it should work.

The reason is that attributes do not magically work. Something must examine them and do things based on them. That code is part of the normal pipeline of MVC when processing a request.

When you call that method directly, you bypass the normal pipeline, thus attributes do not get evaluated and/or run.

Beyond that, just simple common sense should tell you that caching only applies to a response. If there is no response (because you called it directly) it would not apply.

What you should do is execute a child action instead. How you would do that differs depending on where and how you are calling the method. So unless you show us the code that calls the method (and its surrounding context) i can't help you further.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.