我阅读了一个web应用程序的源代码,并看到它使用缓存对象( Web.Caching.Cache)缓存数据。在文件背后的代码( aspx.cs文件)中,它使用Page.Cache来获取缓存,而在其他类中,它使用HttpContext.Current.Cache来获取缓存。我想知道为什么它不使用相同的选项来获得缓存。有人能解释一下Page.Cache和HttpContext.Current.Cache之间的区别吗?为什么在上面的每一个上下文中使用每一个。对于上述两种上下文,我可以使用Page.Cache或HttpContext.Current.Cache吗?提前谢谢。
发布于 2014-05-21 08:26:29
没有区别,前者使用当前页面实例,它使用 property,后者通过HttpContext.Current.Cache使用static方法,这种方法也可以在没有页面实例的静态方法中工作。
两者都是指同一个应用程序缓存。
这样您就可以通过Page获得Page,例如在Page_Load中。
protected void Page_load(Object sender, EventArgs e)
{
System.Web.Caching.Cache cache = this.Cache;
}或者在静态方法中(在HttpContext中使用)通过HttpContext.Current
static void Foo()
{
var context = HttpContext.Current;
if (context != null)
{
System.Web.Caching.Cache cache = context.Cache;
}
}https://stackoverflow.com/questions/23777663
复制相似问题