Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: Which method should be overridden to cache action result

Tags:

c#

asp.net-mvc

I'm preparing to Microsoft Certificate exam (70-515), reading Microsoft book for this exam, practicing tests... one tests asks:

You are creating a custom MVC action filter to cache action results.

Which virtual method should you override?

Correct answer (according to test program, that is distributed with a book) is "OnResultExecuting"

And explanation for the answer:

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

Here is inconsistency: If we need to override the LAST mentioned method, then it should be "OnResultExecuted". But in answer it is told "OnResultExecuting".

So the question is:

  1. What is a CORRECT method to be overridden?
  2. Which option should I choose on exam to get answer considered as correct? (Question is valid for case when "correct" answer is actually different from suggested by system.

Thanks.

P.S. I not sure if current question belongs to SO, but at least it is pretty close

like image 466
Budda Avatar asked Mar 17 '11 01:03

Budda


People also ask

Can we override action method in MVC?

For MVC action command overrides, extend the BaseMVCActionCommand class, and the only method you'll need to override is doProcessAction , which must return void . It's straightforward to override MVC action commands while keeping your code decoupled from the original action methods.

How output cache works in MVC?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

How does MVC handle cache in asp net?

In ASP.NET MVC, there is an OutputCache filter attribute that you can apply and this is the same concept as output caching in web forms. The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory.

What are the action method in MVC?

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.


2 Answers

After some time for me it made some sense: You should override 'OnResultExecuting' method in order to check if you have already result cached. If "yes" you will just get it from cache, if no, you will really execute functionality of "executing" part and then put it to cache.

like image 91
Budda Avatar answered Nov 07 '22 15:11

Budda


The best way would be to look at the source for the built in OutputCacheAttribute. The main guts of it is:

public override void OnResultExecuting(ResultExecutingContext filterContext) {
    if (filterContext == null) {
        throw new ArgumentNullException("filterContext");
    }

    // we need to call ProcessRequest() since there's 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 have implemented it by overriding OnResultExecuting. I personally don't understand why you would wait that long... because the bulk of the time it takes for a request to process would be in the action method with all it's service, repository and whatever calls? No?

Maybe someone much smarter than me can explain.

like image 33
Charlino Avatar answered Nov 07 '22 16:11

Charlino