Search
Close this search box.

How to Configure MaxJsonLength in ASP.NET AJAX applications

Invoking web methods from client scripts is fun. But before you roll out your cool applications into the production, please don’t forget to reconfigure the max JSON string length, represented by either the maxJsonLength value in the web configuration file or the MaxJsonLength property of the JavaScriptSerializer class.

The maximum length of JSON strings, by default is 2097152 characters, which is equivalent to 4 MB of Unicode string data. This should be good enough for most of the web applications. If the size of serialized data you want to send to the client is larger than this default value, you may change it either programmatically or declaratively (in the web.config file of the web site). However, I personally wouldn’t go over the 4MB threshold because serializing a large chunk of data across the wire normally causes huge performance penalty. From my experience, you may want to take a serious look at your application design if you need JSON strings longer than 2097152 characters.

To set this value in your code, you can simply assign an integer to the JavaScriptSerializer.MaxJsonLength property. Alternatively, you can configure the max length in the system.web.extensions configuration section in web.config. The example below sets the maxJswonLength value to 500000 characters. Also bear in mind that the maxJsonLength in the web.config file automatically overrides the JavaScriptSerializer.MaxJsonLength property.

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="500000">
        </jsonSerialization>
      </webServices>
    </scripting>
</system.web.extensions>
This article is part of the GWB Archives. Original Author: Frank Wang`s

Related Posts