Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
When trying to return a large dataset, perhaps an array of items using JSON from both a web service or a controller, you might run into the Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. error.
This is a simple fix for both Web Services and MVC Controllers alike, check below for the fix to both setups.
Increase maxJsonLength in Web Service
Update your web.config with the following:
<configuration> <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="50000000"/> </webServices> </scripting> </system.web.extensions> </configuration>
Increase maxJsonLength in MVC Controller
Place this within your controller.
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior) { return new JsonResult() { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior, MaxJsonLength = Int32.MaxValue }; }
Published at 4 Aug 2016, 10:35 AM
Tags: MVC,Web Service,C#,JSON