C# 在ASP.NET MVC中全局应用JSON序列化程序设置

C# 在ASP.NET MVC中全局应用JSON序列化程序设置,c#,json,json.net,asp.net-web-api2,asp.net-mvc-5.2,C#,Json,Json.net,Asp.net Web Api2,Asp.net Mvc 5.2,实体框架导航当我的剑道网格尝试序列化我的视图模型中的集合时,属性导致“循环对象引用”异常。我们正在使用Newtonsoft.JSON,因此我想(全局)在我的应用程序启动()中将的“ReferenceLoopHandling”设置为“Ignore” 奇怪的是,ASP.NET似乎并不尊重这个设置……所以我还是得到了例外 …有什么想法吗? 版本: Microsoft.AspNet.Mvc(5.2.3.0) Microsoft.AspNet.WebApi(5.2.3) Json(10.0.3) 示例

实体框架导航
当我的
剑道网格
尝试序列化我的
视图模型
中的集合时,属性导致“循环对象引用”异常。我们正在使用
Newtonsoft.JSON
,因此我想(全局)在我的
应用程序启动()中将的“ReferenceLoopHandling”设置为“Ignore”

奇怪的是,ASP.NET似乎并不尊重这个设置……所以我还是得到了例外

…有什么想法吗?

版本:

  • Microsoft.AspNet.Mvc(5.2.3.0)
  • Microsoft.AspNet.WebApi(5.2.3)
  • Json(10.0.3)
示例应用程序\u开始:
移动到调用顶部或底部不会改变结果

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    LoadSiteMap();

    var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}
剃须刀标记示例:
请注意“BindTo”选项

@(Html.Kendo().Grid<RTUDeviceControlRoomAlarm>()
              .Columns(columns =>
              {
                  columns.Bound(x => x.Id)
                      .Visible(false);
                  columns.Bound(x => x.RTUDeviceId)
                      .Visible(false);
                  columns.Bound(x => x.Register)
                      .Title("Register")
                      .Width(50);
                  columns.Bound(x => x.Description)
                      .Title("Description")
                      .Width(100);
                  columns.Bound(x => x.LowLowLimitOFF)
                      .Title("LL Limit/OFF")
                      .Width(50);
                  columns.Bound(x => x.LowLowLimitON)
                      .Title("LL Limit/ON")
                      .Width(50);
                  columns.Bound(x => x.HiLimit)
                      .Title("Hi Limit")
                      .Width(50);
                  columns.Bound(x => x.HiHiLimit)
                      .Title("HH Limit")
                      .Width(50);
                  columns.Command(command => { command.Edit(); command.Destroy(); }).Width(70);
              })
              .Name("gridControlRoomAlarms")
              .ToolBar(toolbar => toolbar.Create())
              .Editable(editable => editable.Mode(GridEditMode.InLine))
              .Sortable()
              .Scrollable()
              .BindTo(Model.RTUDeviceControlRoomAlarms)
              .DataSource(dataSource => dataSource.Ajax()
                                                  .PageSize(50)
                                                  .Model(model => { model.Id(m => m.Id); })
                                                  .Create(update => update.Action("Create", "ControlRoomAlarm", new { Area = "Documents" }))
                                                  .Update(update => update.Action("Update", "ControlRoomAlarm", new { Area = "Documents" }))
                                                  .Destroy(update => update.Action("Destroy", "ControlRoomAlarm", new { Area = "Documents" }))
                                                  )
              .HtmlAttributes(new { @class = "", @style = "height: 400px;" }))
@(Html.Kendo().Grid())
.列(列=>
{
columns.Bound(x=>x.Id)
.可见(假);
columns.Bound(x=>x.RTUDeviceId)
.可见(假);
columns.Bound(x=>x.Register)
.名称(“登记册”)
.宽度(50);
columns.Bound(x=>x.Description)
.标题(“说明”)
.宽度(100);
columns.Bound(x=>x.lowlimitoff)
.标题(“LL限制/关闭”)
.宽度(50);
columns.Bound(x=>x.lowlimiton)
.标题(“所有限制/限制”)
.宽度(50);
columns.Bound(x=>x.HiLimit)
.标题(“上限”)
.宽度(50);
columns.Bound(x=>x.hilimit)
.标题(“HH限额”)
.宽度(50);
Command(Command=>{Command.Edit();Command.Destroy();}).Width(70);
})
.Name(“gridControlRoomAlarms”)
.ToolBar(ToolBar=>ToolBar.Create())
.Editable(可编辑=>Editable.Mode(GridEditMode.InLine))
.Sortable()
.Scrollable()
.BindTo(型号RTU设备控制室警报)
.DataSource(DataSource=>DataSource.Ajax()
.页面大小(50)
.Model(Model=>{Model.Id(m=>m.Id);})
.Create(update=>update.Action(“创建”、“控制室报警”、新建{Area=“Documents”}))
.Update(Update=>Update.Action(“更新”,“控制室报警”,新的{Area=“Documents”}))
.Destroy(update=>update.Action(“Destroy”、“ControlRoomAlarm”、new{Area=“Documents”}))
)
.HtmlAttributes(新的{@class=”,@style=“高度:400px;”}))
更新:
我也试过

  • GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore

尝试将此配置添加到您的
应用程序\u Start()
中,作为最后一行:

json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;

请参阅并小心代码中可能存在的副作用

我是否将“ReferenceLoopHandling”部分与您的建议放在一起?否:“序列化时检测到循环引用”(谢谢)。您对此有何回答?我也遇到了同样的问题。不……还没有。我仍然在经历这个问题。这就是为什么我没有标记这个。