在序列化對象時,我似乎無法阻止Web API/JSON.NET使用Newtonsoft.Json.PreserveReferencesHandling.Objects
。
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
public static class WebApiConfig {
public static void Register (HttpConfiguration config) {
JsonMediaTypeFormatter jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().Single();
jsonFormatter.UseDataContractJsonSerializer = false;
jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
}
}
任何想法:換句話說,$ ID/$裁判總是在序列化的對象,儘管使用以下設置使用?
在''WebApiConfig''類(''公共靜態無效的寄存器設置這個( HttpConfiguration config)從Global.asax.cs中的''protected void Application_Start()''調用 – Michael
根據@AndreHaverdings'下面的答案,將PreserveReferencesHandling設置爲All將導致添加ID和引用 設置最後一個line to 'jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveRef erencesHandling.None;' 應該做的伎倆。 – Buzzwig