MVC: which method should be overridden for the result of the cache action

I am preparing for the Microsoft Certificate exam (70-515), reading the Microsoft book for this exam, practicing the tests ... one test asks:

You create a custom MVC action filter to cache action results.

Which virtual method should be overridden?

The correct answer (according to the test program that is distributed with the book) is "OnResultExecuting"

And an explanation of the answer:

When creating a custom action filter by inheriting the ActionFilterAttribute class, you can override four virtual methods that execute in the following order: OnActionExecuting (), OnActionExecuted (), OnResultExecuting (), and OnResultExecuted (). For output caching, you want final results. Therefore, you must override the last method to run: OnResultExecuting ().

Here's the inconsistency: if we need to override the LAST method, then it must be "OnResultExecuted". But the answer says "OnResultExecuting".

So the question is:

  • What is the CORRECT method for overriding?
  • Which option to choose on the exam to get an answer that is considered correct? (The question is correct for the case when the "correct" answer is really different from the one proposed by the system.

Thanks.

PS I'm not sure the current question belongs to SO, but at least it's pretty close.

+7
source share
5 answers

After some time, it made some sense to me: you should override the OnResultExecuting method to check if you have already reached the caching. If "yes" you just get it from the cache, if not, you really will execute the functionality of the "run" part, and then put it in the cache.

+6
source

The best way is to look at the source for the built-in OutputCacheAttribute . Its main guts:

public override void OnResultExecuting(ResultExecutingContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } // we need to call ProcessRequest() since there no other way to set the Page.Response intrinsic OutputCachedPage page = new OutputCachedPage(_cacheSettings); page.ProcessRequest(HttpContext.Current); } private sealed class OutputCachedPage : Page { private OutputCacheParameters _cacheSettings; public OutputCachedPage(OutputCacheParameters cacheSettings) { // Tracing requires Page IDs to be unique. ID = Guid.NewGuid().ToString(); _cacheSettings = cacheSettings; } protected override void FrameworkInitialize() { // when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here base.FrameworkInitialize(); InitOutputCache(_cacheSettings); } } 

So they implemented it by overriding OnResultExecuting . I personally do not understand why you will wait so long ... because the main part of the time spent on processing the request will be in the method of action with the entire service, repository and any calls? Not?

Maybe someone is much smarter than me can explain.

+3
source

Honestly, I do not agree with this approach. I will personally go override OnActionExecuting and OnResultExecuted. It is not very useful to override OnResultExecuted, since you have already completed the action method at the time the filter is applied. You want to intercept the request before the action is completed, and return the output cache to OnActionExecuting, and you want to commit the final result to OnResultExecuted.

+1
source

Keep in mind that MVC has a built-in [OutputCache] attribute that handles output caching for you: http://www.asp.net/mvc/tutorials/improving-performance-with-output-caching-cs . For real development, this is probably a better approach than creating a custom cache attribute.

+1
source

The agreement says (in relation to the methods for executing events / events): progressive form = before performing a named action and elapsed time = after this has occurred. Therefore, the correct answer really should be OnResultExecuted .

However, I would advise you to contact Microsoft and ask for clarification.

0
source

All Articles