Globally applying JSON Serializer Settings in ASP.NET MVC
Globally applying JSON Serializer Settings in ASP.NET MVC
Entity Framework Navigation
properties are causing a "Circular Object Reference" exception when my Kendo Grid
attempts to serialize a collection in my View Model
. We are using Newtonsoft.JSON
, so I wanted to (globally) set the "ReferenceLoopHandling" to "Ignore" in my Application_Start()
.
Entity Framework Navigation
Kendo Grid
View Model
Newtonsoft.JSON
Application_Start()
Oddly, ASP.NET doesn't seem to be honoring the setting...so I still get the exception.
...ANY IDEAS?
VERSIONS:
SAMPLE APPLICATION_START:
Moving to the call to the top or bottom doesn't change the outcome...
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;
SAMPLE RAZOR MARKUP:
Notice the "BindTo" option...
@(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;" ))
UPDATE:
Have also tried...
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
1 Answer
1
Try adding this configuration in your Application_Start()
as last line:
Application_Start()
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
See https://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm and be careful of possible side effect in your code
NOPE: "A circular reference was detected while serializing" (thanks though)
– Prisoner ZERO
Aug 30 at 16:20
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do I keep the "ReferenceLoopHandling" piece alongside your suggestion?
– Prisoner ZERO
Aug 30 at 16:16