gpt4 book ai didi

asp.net-mvc - JavaScriptSerializer 期间 ASP.NET MVC 中的 MaxJsonLength 异常

转载 作者:行者123 更新时间:2023-12-03 04:37:37 24 4
gpt4 key购买 nike

在我的一个 Controller 操作中,我返回一个非常大的 JsonResult 来填充网格。

我收到以下 InvalidOperationException 异常:

使用 JSON JavaScriptSerializer 进行序列化或反序列化期间出错。字符串的长度超过了 maxJsonLength 属性上设置的值。

遗憾的是,将 web.config 中的 maxJsonLength 属性设置为更高的值不会显示任何效果。

<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>

我不想将其作为字符串传回,如 this 中提到的所以回答。

在我的研究中,我遇到了 this博文中建议编写自己的 ActionResult(例如 LargeJsonResult : JsonResult)来绕过此行为。

这是唯一的解决方案吗?
这是 ASP.NET MVC 中的错误吗?
我错过了什么吗?

任何帮助将不胜感激。

最佳答案

看来这个问题已在 MVC4 中得到修复。

你可以这样做,这对我来说效果很好:

public ActionResult SomeControllerAction()
{
var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}

关于asp.net-mvc - JavaScriptSerializer 期间 ASP.NET MVC 中的 MaxJsonLength 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com