Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  32] [ 5]  / answers: 1 / hits: 13443  / 11 Years ago, wed, december 11, 2013, 12:00:00

I have an MVC3 application running under .NET 4.0 and when I use JavascriptSerializer.Deserialize I'm getting an error.




Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.




Reading Can I set an unlimited length for maxJsonLength in web.config I put the jsonSerialization maxJsonLength key in my web.config but it gets ignored. Though I can set the JavaScriptSerializer.MaxJsonLength property in code and it works fine.



I would like to have the value in the Web.config rather than code. Here is how I am using JavaScriptSerializer.



Dim client As New WebClient
...
Dim result = _client.DownloadString(Test)
Dim serializer = New JavaScriptSerializer
Return serializer.Deserialize(Of Foo)(result)


Here is my Web.config



<configuration>
<configSections>
...
</configSections>
<appSettings>
...
</appSettings>
<system.web>
<customErrors mode=On>
<error statusCode=404 redirect=/Http404/>
<error statusCode=403 redirect=/Http403/>
<error statusCode=550 redirect=/Http550/>
</customErrors>
<compilation debug=true targetFramework=4.0/>
<pages controlRenderingCompatibilityVersion=4.0>
<namespaces>
<add namespace=System.Web.Helpers/>
<add namespace=System.Web.Mvc/>
<add namespace=System.Web.Mvc.Ajax/>
<add namespace=System.Web.Mvc.Html/>
<add namespace=System.Web.Routing/>
<add namespace=System.Web.WebPages/>
</namespaces>
</pages>
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength=50000000/>
</webServices>
</scripting>
</system.web.extensions>
<system.webServer>
....
</system.webServer>
</configuration>

More From » vb.net

 Answers
13

According to the link you provided (the 2nd most voted answer), your web.config setting will be ignored because you're using an internal instance of the JavaScriptSerializer.



If you need the value to be stored in the web.config, you could add a key in the <appSettings> section called maxJsonLength with a value of 50000000 and then in your code, you could use it like:



var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = ConfigurationManager.AppSettings['maxJsonLength'];

[#49632] Tuesday, December 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;