Asp.net MVC输出缓存不RedirectToAction工作
-
20-09-2019 - |
题
有关我的情况下,我有一个控制器,它查询然后使用RedirectResult,这实际上做了报头“位置”转发给用户。
然后我申请缓存以这样控制器
[OutputCache(Duration = int.MaxValue, VaryByParam = "none", NoStore=false)]
我尝试重新运行该页面,我检查我的LINQ的探查,我依然能看到页面的所有查询重新运行在像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;
}
}
不隶属于 StackOverflow