在Asp.net MVC 4中使用OutputCacheAttribute的条件缓存 [英] Conditional Cache using OutputCacheAttribute in Asp.net MVC 4

查看:133
本文介绍了在Asp.net MVC 4中使用OutputCacheAttribute的条件缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的动作结果实现输出缓存.

I am trying to implement Output caching for my action results.

在我根据某些业务规则执行的操作中,将返回响应.在我的回复中,我发送了错误代码. 如果有任何错误,我不想缓存响应.

In my actions depending upon some business rules response is returned. In my response I send error code. I do not want to cache the response if there is any error.

按照操作结果

  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时,我才希望缓存结果.

I want cache the Result only if ErrorCode==0.

我尝试覆盖OutputCache,但无法正常工作

I tried overriding OutputCache, but it is not working

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


    }

还有其他方法或方法来实现这一目标.

Is there any other way or approach to achieve this.

谢谢

推荐答案

您可以创建自己的自定义属性,该属性将根据结果错误代码忽略[OutputCache],如下所示:

You can create your own custom attribute that will ignore [OutputCache] based on the result error code, something like this:

[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 4中使用OutputCacheAttribute的条件缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆