在ASP.NET MVC中实现asynchronous controller操作时,如果我想输出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
});
}
}
首先,我认为它会继续NewsCompleted
,因为这是返回ActionResult
的方法。
然后我意识到NewsAsync
与VaryByParam
相关联,因此将该属性放在该方法上可能更有意义。
答案 0 :(得分:6)
OutputCache
参数取决于void NewsAsync
方法,而不是ActionResult NewsCompleted
方法。 (通过实验确定)