在我的项目中,我使用OutputCache属性,因此我的Web服务器不必一直下载相同的文件,但是很少有一些由于未知原因而返回null的问题。
所以,如果结果为空,我不想缓存结果,是否有一种简单的方法来做到这一点?
[OutputCache(VaryByParam = "path", Duration = 6000)]
public ActionResult LoadCachedFile(string path)
{
var result = DownloadFile(path);
return result;
}
您将必须创建一个从OutputCacheAttribute继承的类,并覆盖
OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
方法。这是一些示例代码:
[System.AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
sealed class CustomOutputCacheAttribute : OutputCacheAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Result != null)
base.OnActionExecuted(filterContext);
}
}