我正在使用jQuery的自动完成功能。当我试图检索超过17000条记录的列表(每个记录不会超过10个字符长度)时,它超过了长度并抛出错误:
异常信息: 异常类型:InvalidOperationException 异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性设置的值。
我可以在web.config中设置maxJsonLength的无限长度吗?如果不是,我可以设置的最大长度是多少?
我正在使用jQuery的自动完成功能。当我试图检索超过17000条记录的列表(每个记录不会超过10个字符长度)时,它超过了长度并抛出错误:
异常信息: 异常类型:InvalidOperationException 异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性设置的值。
我可以在web.config中设置maxJsonLength的无限长度吗?如果不是,我可以设置的最大长度是多少?
当前回答
你不需要使用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=图书版本等等
其他回答
如果你正在使用MVC 4,一定要看看这个答案。
如果你仍然收到错误:
在web.config中将maxJsonLength属性设置为最大值之后 你知道你的数据长度小于这个值 并且您没有使用web服务方法进行JavaScript序列化
你的问题可能是:
MaxJsonLength属性的值仅应用于异步通信层用于调用Web服务方法的内部JavaScriptSerializer实例。(MSDN: ScriptingJsonSerializationSection。MaxJsonLength属性)
基本上,当从web方法调用时,“内部”JavaScriptSerializer尊重maxJsonLength的值;直接使用JavaScriptSerializer(或通过MVC action-method/Controller使用)不尊重maxJsonLength属性,至少不尊重web.config的systemwebextension .scripting. webservices . jsonserialization部分。特别是,Controller.Json()方法不尊重配置设置!
作为一种变通方法,你可以在你的控制器(或任何地方)中执行以下操作:
var serializer = new JavaScriptSerializer();
// For simplicity just use Int32's max value.
// You could always read the value from the config section mentioned above.
serializer.MaxJsonLength = Int32.MaxValue;
var resultData = new { Value = "foo", Text = "var" };
var result = new ContentResult{
Content = serializer.Serialize(resultData),
ContentType = "application/json"
};
return result;
这个答案是我对这个asp.net论坛答案的解释。
如果,在实现上述添加到您的网页。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>
JsonResult result = Json(r);
result.MaxJsonLength = Int32.MaxValue;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
刚碰到这个。我有超过6000条记录。我就决定做点传呼。在我的MVC JsonResult端点中,我接受一个页码,它默认为0,所以它是不必要的,就像这样:
public JsonResult MyObjects(int pageNumber = 0)
然后不要说:
return Json(_repository.MyObjects.ToList(), JsonRequestBehavior.AllowGet);
我说:
return Json(_repository.MyObjects.OrderBy(obj => obj.ID).Skip(1000 * pageNumber).Take(1000).ToList(), JsonRequestBehavior.AllowGet);
这很简单。然后,在JavaScript中,不是这样的:
function myAJAXCallback(items) {
// Do stuff here
}
相反,我说:
var pageNumber = 0;
function myAJAXCallback(items) {
if(items.length == 1000)
// Call same endpoint but add this to the end: '?pageNumber=' + ++pageNumber
}
// Do stuff here
}
然后把你的记录附加到你一开始用它们做的事情上。或者只是等待所有调用完成,然后将结果拼凑在一起。
在MVC 4中,你可以做:
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult()
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
MaxJsonLength = Int32.MaxValue
};
}
在控制器中。
添加:
对于任何对你需要指定的参数感到困惑的人来说,调用可以是这样的:
Json(
new {
field1 = true,
field2 = "value"
},
"application/json",
Encoding.UTF8,
JsonRequestBehavior.AllowGet
);