【发布时间】:2015-04-11 06:05:03
【问题描述】:
我有带有PartialViewResult 或JsonResult 返回类型的控制器。
我想用[OutputCache] 缓存它,但它根本不起作用,并且总是下面的Index 控制器Thread.Sleep(5000); 运行!!!
[HttpPost]
[ValidateAntiForgeryToken]
[OutputCache(Duration = 120, Location = OutputCacheLocation.Server)]
public ActionResult Index(DevicesAjaxViewModel viewModel)
{
try
{
//Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
//Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.AddValidationCallback(IsCacheValid, Request.UserAgent);
#if DEBUG
Thread.Sleep(5000);
#endif
if (!ModelState.IsValid) return Json(new ModelError("Error in Model"));
var allObjects = _objectService.GetAllObjects();
string objectName = allObjects.First(q => q.Id == viewModel.ObjectId).Name;
KeyValuePair<int, List<DeviceModel>> keyValuePair = ApplyFiltering(objectName, viewModel.PageNumber, false, viewModel.Filtering);
FilteringDevicesResultModel filteringDevicesResultModel = new FilteringDevicesResultModel
{
Devices = keyValuePair.Value,
FoundDevicesCount = keyValuePair.Key.ToMoneyFormat(),
RequestId = viewModel.RequestId
};
return PartialView("~/Views/Partials/DevicesPagePartial.cshtml", filteringDevicesResultModel);
}
catch (Exception ex)
{
return Json(new ModelError(ex.Message));
}
}
void IsCacheValid(HttpContext httpContext, object data, ref HttpValidationStatus status)
{
if (true)
status = HttpValidationStatus.Valid;
else
status = HttpValidationStatus.Invalid;
}
我应该如何实现它?
【问题讨论】:
标签: c# asp.net-mvc outputcache