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.
Following in the Action Result
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 };
}
I want cache the Result only if ErrorCode==0.
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.
Thanks