我想在整个项目中缓存连接字符串和使用的缓存对象。我尝试过如下
public static void Demo()
{
Hashtable Hashtable = new Hashtable()
Hashtable.Add("WEBConnectionString",Configurationmanager.ConnectionStrings["WEBConnectionString"].ConnectionString);
HttpContext.Current.Application["CachedValue"] = Hashtable;}
public static string Method(string key)
{
string result = string.Empty;
Hashtable CachedObject = (Hashtable)HttpContext.Current.Application["CachedValue"];
if (CachedObject != null && CachedObject.ContainsKey(key))
{
result = CachedObject[key].ToString();
}
return result;
}
并像这样访问
string conString = Utility.Method("WEBConnectionString");
但CachedObject.ContainsKey(key)
条件变为假。我在这里做错了什么?还是有其他方法可以缓存连接字符串。