0

これは私のクラスです:

public partial class Event
{
    public Event()
    {
        this.Comments = new HashSet<Comment>();
        this.Rates = new HashSet<Rate>();
        this.RawDates = new HashSet<RawDate>();
    }

    public int ID { get; set; }
    public string Title { get; set; }
    public string Summary { get; set; }
    public string SiteURL { get; set; }
    public string ContactEmail { get; set; }
    public string LogoURL { get; set; }
    public int EventType_ID { get; set; }
    public Nullable<int> Location_ID { get; set; }
    public Nullable<System.DateTime> BegginingDate { get; set; }
    public string nTrain { get; set; }
    public string Content { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
    public virtual Conference Conference { get; set; }
    public virtual ICollection<Rate> Rates { get; set; }
    public virtual ICollection<RawDate> RawDates { get; set; }
    public virtual EventType EventType { get; set; }
    public virtual Location Location { get; set; }
}

Web API post メソッドを呼び出すと、タイトルに記載されている例外が次の行でスローされます。

var response = await client.PostAsJsonAsync("api/event", event);

[JsonIgnore]Event クラスのすべての仮想フィールドの上に追加しました。今回はシリアル化が機能しましたが、無視されたフィールドはシリアル化されず、値は null です。Event オブジェクトに含まれるすべての情報が本当に必要です。どうすればこの問題を解決できますか?

4

2 に答える 2

1

WebAPIConfig.cs に以下の構成を追加すると、バグが解決されます。

var json = config.Formatters.JsonFormatter;
//Below configuration to mandatory to resolve the Self referencing loop detected with       
"Newtonsoft.Json.JsonSerializationException" ,
json.SerializerSettings.PreserveReferencesHandling =    
Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
于 2014-04-23T11:29:27.013 に答える
0

循環参照オブジェクトは、JSON シリアル化できません。必要なプロパティを含めるビュー モデルを使用し、アクションで実際のドメイン モデルではなくこのビュー モデルを返すことをお勧めします。

于 2013-03-30T18:27:02.410 に答える