关于System.Web.Caching的“未将对象引用设置到对象的实例”错误

初接触Asp.Net不久,想在后端做个缓存机制,于是使用了System.Web.Caching的Cache类。

使用部分的逻辑很简单,但是取值时总报错

private static Cache cache = new Cache();

public static string LoadCache(string Key)
        {
            if (cache[Key] == null)//这一步报错
            {
                return null;
            }
            else
            {
                return cache[Key].ToString();
            }
        }

网上搜索了一下 解决方法如下:

用 "Cache cache = HttpRuntime.Cache;" 代替 "Cache cache = new Cache();" 来创建Cache的实例。

测试通过,但是原因不明,懂的大神请指点。

posted @ 2014-12-08 15:50  nicky0227  阅读(249)  评论(0编辑  收藏  举报