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.
The
OutputCacheparameter goes on thevoid NewsAsyncmethod, not theActionResult NewsCompletedmethod. (determined by experimentation)