AsyncController OutputCache

时间:2011-02-04 02:26:24

标签: asp.net-mvc-3 outputcache asynccontroller

在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的方法。

然后我意识到NewsAsyncVaryByParam相关联,因此将该属性放在该方法上可能更有意义。

1 个答案:

答案 0 :(得分:6)

OutputCache参数取决于void NewsAsync方法,而不是ActionResult NewsCompleted方法。 (通过实验确定)