在加载大型 JSON 响应表单服务器时,我在 razor View 引擎 MVC4(.net 4.5) 应用程序中遇到以下错误
“使用 JSON JavaScriptSerializer 进行序列化或反序列化期间出错。字符串的长度超出了 @Html.Raw(Json.Encode(jsondata)) 处 maxJsonLength 属性上设置的值”
我尝试在 web.config 中设置 MaxJsonLength 属性:
configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
在发送 JSON 响应的同时尝试在服务器端执行以下操作。
return new JsonResult()
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
MaxJsonLength = Int32.MaxValue
};
还尝试了野兔列出的解决方案:http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/ 。但对我来说没有任何作用:(
有人可以建议我如何避免此错误或如何增加 Jason 响应最大长度吗?
最佳答案
不知何故,我通过在 View 中使用以下代码来消除此错误。
@{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
}
<script type="text/javascript">
var entries = @Html.Raw(serializer.Serialize(Model.FeedEntries));
</script>
至少对我而言,这在服务器端不起作用。
关于jquery - 在MVC 4中增加json响应maxJsonLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288367/