Pergunta

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.

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top