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





Do I keep the "ReferenceLoopHandling" piece alongside your suggestion?
– Prisoner ZERO
Aug 30 at 16:16





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế