asp.net-mvc – 使用Asp.net MVC 4中的OutputCacheAttribute进行条件缓存
发布时间:2020-05-23 02:31:20 所属栏目:asp.Net 来源:互联网
导读:我正在尝试为我的操作结果实现输出缓存. 在我的操作中,根据某些业务规则返回响应.在我的回复中,我发送错误代码.如果有任何错误,我不想缓存响应. 在行动结果中 class Response { public int ErrorCode { get; set; } public string Message { get; set; }} [Ou
|
我正在尝试为我的操作结果实现输出缓存. 在我的操作中,根据某些业务规则返回响应.在我的回复中,我发送错误代码.如果有任何错误,我不想缓存响应. 在行动结果中 class Response
{
public int ErrorCode { get; set; }
public string Message { get; set; }
}
[OutputCache(CacheProfile = "Test")]
public ActionResult Sample()
{
Response response = new Response();
return new JsonResult { Data = response,JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
我想只在ErrorCode == 0时缓存Result. 我尝试重写OutputCache,但它无法正常工作 public class CustomOutputCacheAttribute : OutputCacheAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Result is JsonResult)
{
var result = (JsonResult)filterContext.Result;
BaseReponse response = result.Data as BaseReponse;
if (!response.IsSuccess)
{
filterContext.HttpContext.Response.Cache.SetNoStore();
}
base.OnActionExecuted(filterContext);
}
}
}
有没有其他方法或方法来实现这一目标. 谢谢 解决方法您可以创建自己的自定义属性,根据结果错误代码忽略[OutputCache],如下所示:[OutputCache(Duration=60,VaryByParam="none")]
[OutputCacheValidation]
public ActionResult Sample()
{
var r = new Response();
r.ErrorCode = 0;
return Json(r,JsonRequestBehavior.AllowGet);
}
public class OutputCacheValidationAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
filterContext.HttpContext.Response.Cache.AddValidationCallback(ValidatioCallback,filterContext.Result);
}
private static void ValidatioCallback(HttpContext context,object data,ref HttpValidationStatus validationStatus)
{
var jsonResult = data as JsonResult;
if (jsonResult == null) return;
var response = jsonResult.Data as Response;
if (response == null) return;
if (response.ErrorCode != 0)
{
//ignore [OutputCache] for this request
validationStatus = HttpValidationStatus.IgnoreThisRequest;
context.Response.Cache.SetNoServerCaching();
context.Response.Cache.SetNoStore();
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 谁设置HttpContext.User.Identity的IsAuthe
- 认证 – WebApi ActionFilterAttribute,HttpActionContext访
- asp.net-mvc-4 – Azure Blob 400创建容器时的错误请求
- 如何从asp.net按钮单击事件调用javascript函数
- ASP.NET的临时文件下载链接
- 如何在ASP.NET中为不同的URL设置不同的超时
- asp.net-core – 在ASP.net Core中使用BeginCollectionItem
- asp.net – 使用installshield在安装后运行解决方案exe
- asp.net-mvc-3 – 具有最佳实践的示例N层ASP.NET MVC3应用程
- ASP.NET MVC应用程序处理程序映射和模块的性能和安全性
推荐文章
站长推荐
- asp.net-mvc – 如何在我的MVC应用程序有机会处理
- asp.net-mvc – Url.Action with RouteValueDict
- asp.net – %#Eval(“State”)%或%#DataBinde
- asp.net – DropDownList的EditorTemplate
- asp.net – Databinder.Eval和Container.DataIte
- asp.net-mvc – ViewModel中的MVC3 RouteUrl
- asp.net – Linq更新查询生成哪里0 = 1?
- ASP.NET Web API操作使用接口而不是具体类
- asp.net-mvc – Sitecore在我的MVC解决方案中提供
- asp.net-mvc – “安全感知”动作链接?
热点阅读
