我正在使用jQuery的自动完成功能。当我试图检索超过17000条记录的列表(每个记录不会超过10个字符长度)时,它超过了长度并抛出错误:

异常信息: 异常类型:InvalidOperationException 异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性设置的值。

我可以在web.config中设置maxJsonLength的无限长度吗?如果不是,我可以设置的最大长度是多少?


当前回答

只需在MVC的动作方法中设置MaxJsonLength属性

JsonResult json= Json(classObject, JsonRequestBehavior.AllowGet);
json.MaxJsonLength = int.MaxValue;
return json;

其他回答

如果这个maxJsonLength值是int,那么它的int有多大32位/64位/16位....我只是想确定我可以设置为我的maxJsonLength的最大值

<scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483647">
            </jsonSerialization>
        </webServices>
    </scripting>

对于那些有问题的MVC3 JSON自动反序列化的模型绑定和太大,这里有一个解决方案。

将JsonValueProviderFactory类的代码从MVC3源代码复制到一个新类中。 添加一行来更改对象反序列化之前的最大JSON长度。 用修改后的新类替换JsonValueProviderFactory类。

感谢http://blog.naver.com/techshare/100145191355和https://gist.github.com/DalSoft/1588818为我指明了正确的方向,如何做到这一点。第一个站点上的最后一个链接包含解决方案的完整源代码。

我使用这个,它为剑道网格读取请求工作。

{ 
  //something
   var result = XResult.ToList().ToDataSourceResult(request);
   var rs = Json(result, JsonRequestBehavior.AllowGet);
   rs.MaxJsonLength = int.MaxValue;
   return rs;
}

如果,在实现上述添加到您的网页。config,你会得到一个“无法识别的配置部分system.web.extensions”。的错误,然后尝试将此添加到您的网页。<ConfigSections> section中的config:

            <sectionGroup name="system.web.extensions" type="System.Web.Extensions">
              <sectionGroup name="scripting" type="System.Web.Extensions">
                    <sectionGroup name="webServices" type="System.Web.Extensions">
                          <section name="jsonSerialization" type="System.Web.Extensions"/>
                    </sectionGroup>
              </sectionGroup>
        </sectionGroup>

你不需要使用web.config 您可以在传递列表的catch值期间使用short属性 例如 像这样声明一个模型

public class BookModel
    {
        public decimal id { get; set; }  // 1 

        public string BN { get; set; } // 2 Book Name

        public string BC { get; set; } // 3 Bar Code Number

        public string BE { get; set; } // 4 Edition Name

        public string BAL { get; set; } // 5 Academic Level

        public string BCAT { get; set; } // 6 Category
}

这里我用的是短比例 公元前=条形码 BE=图书版本等等