简体   繁体   中英

MaxJsonLength exception in ASP.NET MVC4 during JavaScriptSerializer

In one of my controller actions,when I return a more than 10000 rows JsonResult to the view. I will get the below exceptions.

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. 在此处输入图片说明

I have setted the maxJsonLength property in the web.config to a higher value unfortunately does not show any effect like below:

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

And I also set the MaxJsonLength when I used Json class like below:

    public JsonResult GetList(GridPager pager, string queryStr)
    {
        List<SysExceptionModel> list = _exceptionBLL.GetList(ref pager, queryStr);
        var json = new
        {
            total = pager.totalRows,
            rows = list.ToArray()
        };

        var jsonresults = Json(json);
        jsonresults.MaxJsonLength = int.MaxValue;
        return jsonresults;
    }

These two settings would not help me. I was confused.

one row is like below:

0000688A-85F5-4F72-BE86-85BCADED2DE NULL BBBB BBBB BBBB BBBB BBBB 2015-10-22 02:27:38.000

Try to use NewtonSoft.Json library instead. Or try to override the JsonResult like in this example: CustomJsonResult

Setting the max jsonSerialization in the xml does not always work.

In your first example, try setting the MaxJsonLength property directly on the JavaScriptSerializer instance. Per the documentation , it does not use the jsonSerialization element of the configuration file when you explicitly create an instance of the serializer.

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = int.MaxValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM