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

时间:2018-08-30 15:39:36

标签: c# json json.net asp.net-web-api2 asp.net-mvc-5.2

当我的Entity Framework Navigation尝试序列化我的Kendo Grid中的集合时,

View Model属性导致“循环对象引用” 异常。我们正在使用Newtonsoft.JSON,所以我想(在全局上)在我的Application_Start()中将“ ReferenceLoopHandling” 设置为“忽略”

奇怪的是,ASP.NET似乎并没有遵守该设置...因此我仍然遇到异常。

...任何想法?

版本:

  • Microsoft.AspNet.Mvc(5.2.3.0)
  • Microsoft.AspNet.WebApi(5.2.3)
  • Newtonsoft.Json(10.0.3)

样品申请_START:
移至 top 底部的调用不会改变结果...

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;" }))

更新:
也尝试过...

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

1 个答案:

答案 0 :(得分:0)

尝试在您的Application_Start()的最后一行中添加此配置:

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

请参见https://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm,并注意代码中可能出现的副作用