2012-05-10 3 views
9
[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;   
} 

私はasp.netのuserprofileに保存しようとしていますが、それも保存しますが、私はそれにアクセスしているときに、キーと値の両方のプロパティを表示しますnull、誰かが私が間違っている場所を教えてください?KeyValuePair <K,V>をユーザープロファイルに保存できますか?

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> 
+0

私はあなたのプロパティから別の名前を付けることを試みます。つまり、 '[" _KV "]'などを保存して読んでみてください。なぜそれが違いがあるのか​​想像できませんが、 – Rup

+0

新しいMVC3プロジェクトでこれを再現することはできません(つまり正常に動作します)。あなたは完全なテストケースを提供できますか?それ以外の場合は、フローを確認することをお勧めします。おそらくLoadProfile()を呼び出している時点で値がありません。 – georgiosd

答えて

0

あなたのクラスとKeyValuePairプロパティの属性 [DataMemberを] [のDataContract]を配置してみます。 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; } 
    }    
} 
2

C#のKeyValuePairキー/値の属性のための公共のセッターを持っていません。したがって、シリアライズするかもしれませんが、それは空のデシリアライズします。

[Serializable] 
[DataContract] 
public class KeyValue<K,V> 
{ 
    /// <summary> 
    /// The Key 
    /// </summary> 
    [DataMember] 
    public K Key { get; set; } 

    /// <summary> 
    /// The Value 
    /// </summary> 
    [DataMember] 
    public V Value { get; set; } 
} 

をそしてあなたの例では、それを使用します。

次の例のように、クラスのあなた自身の小さな実装を作成する必要があります。

関連する問題