If you are using MVC 4, be sure to check out this answer as well.
If you are still receiving the error:
maxJsonLength
property to its maximum value in web.configyour problem is is likely that:
The value of the MaxJsonLength property applies only to the internal JavaScriptSerializer instance that is used by the asynchronous communication layer to invoke Web services methods. (MSDN: ScriptingJsonSerializationSection.MaxJsonLength Property)
Basically, the "internal" JavaScriptSerializer
respects the value of maxJsonLength
when called from a web method; direct use of a JavaScriptSerializer
(or use via an MVC action-method/Controller) does not respect the maxJsonLength
property, at least not from the systemWebExtensions.scripting.webServices.jsonSerialization
section of web.config. In particular, the Controller.Json()
method does not respect the configuration setting!
As a workaround, you can do the following within your Controller (or anywhere really):
var serializer = new JavaScriptSerializer();
// For simplicity just use Int32's max value.
// You could always read the value from the config section mentioned above.
serializer.MaxJsonLength = Int32.MaxValue;
var resultData = new { Value = "foo", Text = "var" };
var result = new ContentResult{
Content = serializer.Serialize(resultData),
ContentType = "application/json"
};
return result;
This answer is my interpretation of this asp.net forum answer.