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
        });
    }
}

起初,我认为它会继续NewsCompleted,因为那是返回一个ActionResult.

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

4

1 回答 1

6

OutputCache参数在方法上,而void NewsAsync不是ActionResult NewsCompleted方法上。(由实验确定)

于 2011-02-04T02:53:19.673 回答