我收到了这个错误:
内部服务器错误:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。
我已经搜索了这个答案并找到了2个答案,这些答案似乎都不适合我。我试图将maxJsonLength设置添加到web.config,但它被忽略了。同样,我已经尝试在C#代码中设置MaxJsonLength = int.MaxValue;但我使用MVC 2.0,它似乎只适用于MVC 4?任何人都可以帮我正确设置maxJsonLength属性吗?这是我收到错误的地方。
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var result = filterContext.Result as ViewResult;
if (result == null)
{
return;
}
var jsonResult = new JsonResult
{
Data = result.ViewData.Model,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
filterContext.Result = jsonResult;
}
答案 0 :(得分:1)
我不确定它是否有效,但我找到了一些解决方案,请尝试以下
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
或
public ActionResult SomeControllerAction()
{
var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
答案 1 :(得分:1)
知道了,使用以下代码
public ActionResult MyFancyMethod()
{
var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };
var result = new ContentResult
{
Content = serializer.Serialize(myBigData),
ContentType = "application/json"
};
return result;
}