Asp.net mvc输出缓存不适用于RedirectToAction

时间:2010-01-14 02:04:18

标签: asp.net-mvc outputcache

对于我的情况,我有一个控制器,然后查询然后使用RedirectResult转发用户,该实际上执行了标题“位置”。

然后我将缓存应用于控制器,如此

[OutputCache(Duration = int.MaxValue, VaryByParam = "none", NoStore=false)]

我尝试重新运行该页面,并且检查了我的Linq分析器,我仍然能够看到该页面的所有查询重新运行,如同1s。

如何防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

您可以执行手动缓存,而不是使用输出缓存,这将缓存您的查询:

public IQueryable<Category> FindAllCategories()
{
    if (HttpContext.Current.Cache["AllCategories"] != null)
        return (IQueryable<Category>)HttpContext.Current.Cache["AllCategories"];
    else
    {
        IQueryable<Category> allCats =  from c in db.Categories
                                          orderby c.Name
                                          select c;

        // set cache
        HttpContext.Current.Cache.Add("AllCategories", allCats, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 30, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
        return allCats;
    }
}
相关问题