实现 asynchronous controller 时ASP.NET MVC 中的 action,如果我想输出缓存 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
关联,因此将属性放在该方法上可能更有意义。
最佳答案
OutputCache
参数用于 void NewsAsync
方法,而不是 ActionResult NewsCompleted
方法。 (通过实验确定)
关于asp.net-mvc-3 - 异步 Controller 输出缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893964/