我们可以在用户个人资料中保存KeyValuePair <K,V>吗?

Gau*_*rav 9 .net c# asp.net-profiles asp.net-mvc-3

[Serializable]
public class KeyValue : ProfileBase
{
    public KeyValue() { }

    public KeyValuePair<string, string> KV
    {
        get { return (KeyValuePair<string, string>)base["KV"]; }
        set { base["KV"] = value; }
    }            
}

public void SaveProfileData()
{
    KeyValue profile = (KeyValue) HttpContext.Current.Profile;
    profile.Name.Add(File);
    profile.KV = new KeyValuePair<string, string>("key", "val"); 
    profile.Save();
}   

public void LoadProfile()
{
    KeyValue profile = (KeyValue) HttpContext.Current.Profile;
    string k = profile.KV.Key;
    string v = profile.KV.Value;
    Files = profile.Name;          
}
Run Code Online (Sandbox Code Playgroud)

我试图保存KeyValuePair<K,V>在asp.net userprofile中,它也保存但是当我访问它时,它显示键和值属性null,任何人都可以告诉我我错在哪里?

LoadProfile()k和v中为空.

Web.config文件

<profile enabled="true" inherits="SiteBuilder.Models.KeyValue">
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
</profile>
Run Code Online (Sandbox Code Playgroud)

ala*_*lan 0

尝试将[DataContract][DataMember]属性放置在您的类和 KeyValuePair 属性上。您需要添加对System.Runtime.Serialization的引用。请记住,您可能还需要在基类级别应用这些属性才能使序列化正常工作。

[DataContract]
public class KeyValue : ProfileBase
{
    public KeyValue() { }

    [DataMember]
    public KeyValuePair<string, string> KV
    {
        get { return (KeyValuePair<string, string>)base["KV"]; }
        set { base["KV"] = value; }
    }            
}
Run Code Online (Sandbox Code Playgroud)