3

I am writing to you with an error in website: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Used technologies: C #,. NET FW 4.5, ASP.NET MVC4 and Lint to SQL, Kendo UI (grid for show result).

I would like to return (in terms of Json) a large set of data - actually I have 50,000 records (more than 250,000 records would not occur)

I try enlarge maxJsonLength and future enable compression in web.config - with same error:

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="10485760"/>
      </webServices>
      <scriptResourceHandler enableCompression="true" enableCaching="true"/>
    </scripting>
  </system.web.extensions>

Next I try rewrite return method in C# class - with same error: a) default

public JsonResult GetResult()
{
  // execute query for get result
  var myBigData = from ......
      select new
      {
          .......
      };

  // return result
  return this.Json(myBigData, JsonRequestBehavior.AllowGet);
}

b) rewriten (still error)

public JsonResult GetResult()
{
  // execute query for get result
  var myBigData = from ......
      select new
      {
          .......
      };

  // return result
  var jsonResult = Json(myBigData, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}

To eliminate the mistakes I tried a smaller number of data - all is right: Test which is correct: 1 - 10 of 6999 items

Best regards, Peter

Note: Unfortunately, I went through a lot of discussion (here), but without success, so I apologize for any duplicate topic.

1 Answer 1

5

It seems that value from web.config is ignored, and according to documentation you have to explicitly create an instance of JavascriptSerializer

public ContentResult GetResult() 
{
    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var result = new ContentResult();
    result.Content = serializer.Serialize(data);
    result.ContentType = "text/json";
    return result;
}

At least it works for me :)

Sign up to request clarification or add additional context in comments.

1 Comment

I've just found a better solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.