ASP.net中的缓存

ASP.net中的缓存,asp.net,Asp.net,我有一个网络服务。 因为我使用的是缓存 我编写了以下代码来将datatable存储在缓存中 using System.Web.Caching; Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null); 它给我的错误是“非静态字段需要对象引用” 如何删除此错误使用.Insert(…)。此“缓存”是返回Syst

我有一个网络服务。 因为我使用的是缓存

我编写了以下代码来将datatable存储在缓存中

using System.Web.Caching;

Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
它给我的错误是“非静态字段需要对象引用”

如何删除此错误使用.Insert(…)。此“缓存”是返回System.Web.Caching.Cache实例的属性。它与HttpContext.Current.Cache相同,但不需要HttpContext

您的代码试图在不使用实例的情况下访问该类的方法,因此出现错误消息。

使用.Insert(…)。此“缓存”是返回System.Web.Caching.Cache实例的属性。它与HttpContext.Current.Cache相同,但不需要HttpContext


您的代码试图在不使用实例的情况下访问该类的方法,因此出现了错误消息。

您试图将缓存类用作静态类

如果希望在请求期间为HTTP上下文使用当前缓存类实例,则应执行以下操作:

HttpContext.Current.Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);

您正在尝试将缓存类用作静态类

如果希望在请求期间为HTTP上下文使用当前缓存类实例,则应执行以下操作:

HttpContext.Current.Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);