AsyncController OutputCache

1.2k views Asked by At

When implementing an asynchronous controller action in ASP.NET MVC, if I want to output cache the ActionResult, which method do I put the OutputCache attribute on?

public class PortalController : AsyncController {
    /// HERE...?
    [OutputCache(Duration = 60 * 30 /* 30min */, VaryByParam = "city")]
    public void NewsAsync(string city) {

        AsyncManager.OutstandingOperations.Increment();
        NewsService newsService = new NewsService();
        newsService.GetHeadlinesCompleted += (sender, e) =>
        {
            AsyncManager.Parameters["headlines"] = e.Value;
            AsyncManager.OutstandingOperations.Decrement();
        };
        newsService.GetHeadlinesAsync(city);
    }

    /// ...OR HERE?
    [OutputCache(Duration = 60 * 30 /* 30min */, VaryByParam = "city")]
    public ActionResult NewsCompleted(string[] headlines) {
        return View("News", new ViewStringModel
        {
            NewsHeadlines = headlines
        });
    }
}

At first, I assumed that it would go on NewsCompleted, because that is the method which returns an ActionResult.

Then I realized that NewsAsync is associated with VaryByParam, so it probably makes more sense to put the attribute on that method.

1

There are 1 answers

0
Jacob Krall On BEST ANSWER

The OutputCache parameter goes on the void NewsAsync method, not the ActionResult NewsCompleted method. (determined by experimentation)