AsyncController OutputCache

Jac*_*all 3 outputcache asynccontroller asp.net-mvc-3

在ASP.NET MVC中实现异步控制器操作时,如果我想输出缓存ActionResult,我将该OutputCache属性放在哪个方法上?

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
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

起初,我认为它会继续下去NewsCompleted,因为这是返回的方法ActionResult.

然后我意识到它NewsAsync与之相关VaryByParam,因此将属性放在该方法上可能更有意义.

Jac*_*all 6

OutputCache参数的推移的void NewsAsync方法,而不是ActionResult NewsCompleted方法.(通过实验确定)