AsyncController OutputCache

When implementing an asynchronous controller in ASP.NET MVC, if I want to display the cache ActionResult, which method did I put the attribute OutputCacheon?

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 suggested that this would continue NewsCompletedbecause it is a method that returns ActionResult.

Then I realized what was NewsAsyncconnected with VaryByParam, so it probably makes sense to add an attribute to this method.

+3
source share
1 answer

The parameter OutputCacheapplies to the method void NewsAsync, not to the method ActionResult NewsCompleted. (determined by experiments)

+6
source

Source: https://habr.com/ru/post/1789896/


All Articles