缓存对象类型转换错误

本文关键字:错误 类型转换 对象 缓存 | 更新日期: 2023-09-27 17:54:10

if (System.Web.HttpContext.Current.Cache.Get("dsActiveNews_FixID_" + newsFixID) == null)
                news = NewsDB.getNewsBodyByFixID(newsFixID);
                System.Web.HttpContext.Current.Cache.Add("dsActiveNews_FixID_" + newsFixID, news, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
            news = (News)(System.Web.HttpContext.Current.Cache.Get("dsActiveNews_FixID_" + newsFixID));

第一次当我调用的页面铸造工作,当我刷新页面,我得到一个异常InvalidCastException: Specified cast is not valid

缓存对象类型转换错误

可能是null/或其他对象,并且没有强制转换,所以获取它并检查为

var oNews = System.Web.HttpContext.Current.Cache.Get("dsActiveNews_FixID_" + newsFixID) as News;
if(oNews != null)
{    
}