Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON, ASP.NET MVC - MaxJsonLength exception

I am just trying to shift some comma separated numbers to the frontend:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetSquares()
{
 var result = new JsonResult();
 result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
IList<double> list = new List<double>();
...
result.Data = list;
return result;
}

This works fine as long as there are only a few numbers. Unfortunately, I have to shift lots of numbers occasionally and get a MaxJsonLength exception. I tried several suggestions to overcome this (add something to the web.config file etc.). Maybe I do not have to use JSON after all? However I still 'have to do something' with the numbers using javascript. I am using jquery's ajax stuff at the moment.

Any suggestions welcome ...

like image 563
cs0815 Avatar asked Nov 11 '10 13:11

cs0815


People also ask

What is the maximum value for MaxJsonLength?

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

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.

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).


2 Answers

Here is another custom JsonResult (CorrectJsonResult) which handles larger serialization limits that the default 4MB allowed by JavascriptConverter.

And another example which uses ContentResult instead of JsonResult subclass.

public ActionResult GetLargeJsonResult()
{
  return new ContentResult
  {
    Content = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue }.Serialize(myBigdata),
    ContentType = "application/json"
  };
}
like image 119
SliverNinja - MSFT Avatar answered Oct 16 '22 14:10

SliverNinja - MSFT


I have extended the base class Controller and work great:

ControllerExtensions class:

namespace SCAWEB.Helpers
{
    public static class ControllerExtensions
    {
        #region Json
        public static int MaxJsonLength{get;set;}

        static ControllerExtensions()
        {
            MaxJsonLength = 2147483644;
        }

        public static System.Web.Mvc.JsonResult LargeJson(this System.Web.Mvc.Controller controlador, object data)
        {
            return new System.Web.Mvc.JsonResult()
            {
                Data = data,
                MaxJsonLength = MaxJsonLength,
            };
        }
        public static System.Web.Mvc.JsonResult LargeJson(this System.Web.Mvc.Controller controlador, object data, System.Web.Mvc.JsonRequestBehavior behavior)
        {
            return new System.Web.Mvc.JsonResult()
            {
                Data = data,
                JsonRequestBehavior = behavior,
                MaxJsonLength = MaxJsonLength
            };
        }
        //TODO: You can add more overloads, the controller class has 6 overloads
        #endregion
    }
}

MyController class:

using SCAWEB.Helpers;

namespace SCAWEB.Controllers
{
    public class VentasController : Controller
    {
        public ActionResult VentasList (){
            //Todo: All the action code

            //return this.Json(myData);
            return this.LargeJson(myData);//Here I use my extensions
        }
    }
}

You can especify the max length in your code:

ControllerExtensions.MaxJsonLength = 1073741824;//1GB
like image 23
Juan Carlos Velez Avatar answered Oct 16 '22 15:10

Juan Carlos Velez