Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase json response maxJsonLength in MVC 4

I am getting following error in razor view engine MVC4(.net 4.5) application when loading a large JSON response form server at

Error during serialization or deserialization using the JSON JavaScriptSerializer.The length of the string exceeds the value set on the maxJsonLength property at @Html.Raw(Json.Encode(jsondata)”

I have tried by setting MaxJsonLength property in my web.config:

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

Tried following at server side while sending JSON response as well.

 return new JsonResult()
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior,
        MaxJsonLength = Int32.MaxValue
    };

Also tried the solution listed hare: http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/. But nothing worked for me :(

Can some suggest me how to avoid this error or how to increase the Jason response max length?

like image 660
Nabeel Avatar asked Mar 08 '13 06:03

Nabeel


People also ask

What is the maximum value for MaxJsonLength?

The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.

Can I set the unlimited length for MaxJsonLength property in config?

The MaxJsonLength property cannot be unlimited, is an integer property that defaults to 102400 (100k).

What is Jsonserialization MaxJsonLength?

The MaxJsonLength property which can be set within the web. config of your application controls the maximum size of the JSON strings that are accepted by the JsonSerializer class. The default value is 102400 characters.


3 Answers

Somehow I get rid of this error by using following code in view.

@{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
}
<script type="text/javascript">
var entries = @Html.Raw(serializer.Serialize(Model.FeedEntries));    
</script>

This was not working at server side at least for me.

like image 99
Nabeel Avatar answered Sep 22 '22 22:09

Nabeel


My penny to the solutions. Did b) because a) gave errormessage 'System.Web.Mvc.JsonResult does not contain a definition for maxJsonLength ... ' in Mvc 4.5 AFAIK, this is the only workaround that works.

I put b) in my controller. Hopefully this will help someone.

Regards, SM

a)

var jsonResult = Json(list, JsonRequestBehavior.AllowGet);
jsonResult.maxJsonLength = int.MaxValue;
return jsonResult;

b)

if (Request.IsAjaxRequest())
{
   //Working solution
   var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };

   return new ContentResult()
   {
      Content = serializer.Serialize(list),
      ContentType = "application/json",
   };

   //Trial 2
   //var jsonResult = Json(list, JsonRequestBehavior.AllowGet);
   //jsonResult.maxJsonLength = int.MaxValue;
   //return jsonResult;

   //Trial 1
   //return Json(list, JsonRequestBehavior.AllowGet);
} 
like image 34
Emperor 2052 Avatar answered Sep 26 '22 22:09

Emperor 2052


This worked to me

   return new JsonResult()
            {
                Data=jsonData,
                MaxJsonLength = 86753090,
                JsonRequestBehavior=JsonRequestBehavior.AllowGet
            };
like image 21
Ahamed Ishak Avatar answered Sep 22 '22 22:09

Ahamed Ishak