Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncController OutputCache

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.

like image 616
Jacob Krall Avatar asked Dec 11 '25 04:12

Jacob Krall


1 Answers

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

like image 88
Jacob Krall Avatar answered Dec 13 '25 23:12

Jacob Krall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!