Handling larger JSON string values serialization in MVC
Some days back in a project there was a need to fetch AD users and groups from MVC controller in JSON format at run time. I chose return type of controller method as “JsonResult” instead of “ActionResult” and called this method using jQuery ajax call.
In this method, users and groups come from AD (Active Directory). It ideally results specified set of users as well as groups.
The code for calling MVC method using ajax call:
Configuration setting in web.config:
Now the problem that arises is – after some time, number of AD users and groups increase, and then when fetching the same, an error occurs due to the length of the string:
This happens due to fetching of large amount of data in JSON format via ajax call.
So, here are the solution options to it:
The common, quick and easy solution for this error is to update the “jsonSerialization” configuration to maximum as below:
But, in my case the issue persisted after configuration updates because the JSON received at the success of ajax call was not getting serialized, and was going beyond maximum limit.
This issue can also be resolved by using “ContentResult” as return type instead of “JsonResult” and serializing the object before returning to the ajax call.
The second solution worked for me, and now I can receive lacs of records from my action method.