ASP.Net Increase Maximum JSON response size

January 16, 2023
 
Update maxJsonLength for Web.Config and JavaScriptSerializer
Typically when using JSON responses from web services your JSON should be a smaller size, but if you ever have the need to get larger JSON responses, here is how you can do it. You need to increase the allowed maximum JSON length size in the Web.Config and you also need to increase the maximum JSON size in the JsonResult.

Increase the maxJsonLength in the Web.Config

<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>

Increase the MaxJsonLength on the JsonResult

You can override the default behavior of the JsonResult.

protected overrride JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult()
{
Data = data,
ContentType = contentType
JsonRequestBehavior = behavior
MaxJsonLength = Int32.MaxValue };
}

 
Return to articles