如何在asp.net配置文件中存储名称 - 值对

时间:2012-04-26 09:45:22

标签: c# asp.net

  

可能重复:
  Store a generic dictionary in an asp.net profile?

我想在asp.net配置文件中保存名称 - 值对,我该怎么做?

1 个答案:

答案 0 :(得分:1)

定义一个可以保存键值数据的类,如:

namespace Samples.AspNet.Profile 
{
    [Serializable]
    public class KeyValue{

        public Dictionary<string, Object> dict= new Dictionary<string, Object>();
    }

}

在KeyValue类型的配置中初始化配置文件:

<profile defaultProvider="AspNetSqlProfileProvider">
  <properties>
    <add name="List" 
      type="Samples.AspNet.Profile.KeyValue" 
      serializeAs="Binary" />
  </properties>
</profile>

现在您可以添加如下值:

KeyValue data= new KeyValue ();
data.dict.Add("Contoso","value");
bookCart.CartItems.Add("Microsoft","value2");
Profile.List= data;