JavaScriptSerializer 期间 ASP.NET MVC 中的 MaxJsonLength 异常
在我的一个控制器操作中,我返回一个非常大的 JsonResult
来填充网格。
我收到以下 InvalidOperationException
异常:
使用 JSON JavaScriptSerializer 进行序列化或反序列化期间出错。字符串的长度超过了 maxJsonLength 属性设置的值。
将 web.config
中的 maxJsonLength
属性设置为更高的值很遗憾,不会显示任何内容影响。
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
我不想将其作为字符串传递回来,如 this 中提到的所以回答。
在我的研究中,我遇到了 这篇 博文,建议编写自己的 ActionResult
(例如 LargeJsonResult : JsonResult
)绕过此行为。
这是唯一的解决方案吗?
这是 ASP.NET MVC 中的错误吗?
我错过了什么吗?
任何帮助将不胜感激。
In one of my controller actions I am returning a very large JsonResult
to fill a grid.
I am getting the following InvalidOperationException
exception:
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Setting the maxJsonLength
property in the web.config
to a higher value unfortunately does not show any effect.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
I don't want to pass it back as a string as mentioned in this SO answer.
In my research I came across this blog post where writing an own ActionResult
(e.g. LargeJsonResult : JsonResult
) is recommended to bypass this behaviour.
Is this then the only solution?
Is this a bug in ASP.NET MVC?
Am I missing something?
Any help would be most appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
看来这个问题已在 MVC4 中得到修复。
你可以这样做,这对我来说效果很好:
It appears this has been fixed in MVC4.
You can do this, which worked well for me:
您还可以使用
ContentResult
< /a> 作为 此处建议而不是子类化JsonResult
。You could also use
ContentResult
as suggested here instead of subclassingJsonResult
.不幸的是,web.config 设置是 被默认 JsonResult 实现忽略。所以我想你需要实现一个自定义的 json 结果来解决这个问题。
Unfortunately the web.config setting is ignored by the default JsonResult implementation. So I guess you will need to implement a custom json result to overcome this issue.
不需要自定义类。这就是所需的全部内容:
其中
Result
是您希望序列化的数据。No need for a custom class. This is all that is needed:
where
Result
is that data you wish to serialize.替代 ASP.NET MVC 5 修复:
在我的例子中,错误是在请求期间发生的。我的场景中的最佳方法是修改实际的
JsonValueProviderFactory
,它将修复应用到全局项目,并且可以通过编辑global.cs
文件来完成。添加一个 web.config 条目:
然后创建以下两个类
这基本上是 System.Web.Mvc 中默认实现的精确副本,但添加了可配置的 web.config appsetting 值
aspnet:MaxJsonLength
。Alternative ASP.NET MVC 5 Fix:
In my case the error was occurring during the request. Best approach in my scenario is modifying the actual
JsonValueProviderFactory
which applies the fix to the global project and can be done by editing theglobal.cs
file as such.add a web.config entry:
and then create the two following classes
This is basically an exact copy of the default implementation found in
System.Web.Mvc
but with the addition of a configurable web.config appsetting valueaspnet:MaxJsonLength
.我很惊讶没有人建议使用结果过滤器。这是全局挂接到操作/结果管道的最简洁的方法:
然后,使用 GlobalFilters.Filters 注册该类的实例:
I'm surprised no one has suggested using a result filter. This is the cleanest way to globally hook into the action/result pipeline:
Then, register an instance of that class using
GlobalFilters.Filters
:如果使用 Json.NET 生成
json
字符串,则不需要设置 MaxJsonLength 值。If use Json.NET to generate the
json
string, it doesn't need to setMaxJsonLength
value.还有一种其他情况 - 数据从客户端发送到服务器。
当您使用控制器方法并且模型很大时:
系统会抛出如下异常“使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超出了 maxJsonLength 属性上设置的值。参数名称:输入”
仅更改 Web。在这种情况下,配置设置不足以提供帮助。您还可以覆盖 mvc json 序列化程序以支持巨大的数据模型大小或从 Request 手动反序列化模型。您的控制器方法变为:
there is a bit other case - data is sent from client to server.
when you are using controller method and model is huge :
system throws exception like this "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Parameter name: input"
Only changing Web.config settings is not enough to help in this case. You could additionally override mvc json serializer for supporting huge data model sizes or manually deserialize model from Request. Your controller method becomes:
我按照此解决了问题 链接
I solved the issue by following this link
您可以尝试在 LINQ 表达式中仅定义您需要的字段。
例子。想象一下,您有一个包含 ID、名称、电话和图片(字节数组) 的模型,并且需要从 json 加载到选择列表中。
LINQ 查询:
这里的问题是获取所有字段的“select u”。所以,如果你有大图片,那就太好了。
怎么解决?非常非常简单。
最佳实践是仅选择您将使用的字段。
记住。这是一个简单的技巧,但可以帮助许多 ASP.NET MVC 开发人员。
You can try define in your LINQ expression only the field's that you will need.
Example. Imagine that you have an Model with Id, Name, Phone and Picture (byte array) and need to load from json into an select list.
LINQ Query:
The problem here is "select u" that get all fields. So, if you have big pictures, booomm.
How to solve? very, very simple.
The best practices is select only the field that you will use.
Remember. This is a simple tip, but can help many ASP.NET MVC developpers.
MVC 4 中的修复对我来说是这样的。
Was the fix for me in MVC 4.
在我将操作更改为
[HttpPost]
之前,上述所有操作都不适合我。并将ajax类型设为
POST
。ajax 调用为
None of the above worked out for me until I changed the Action as
[HttpPost]
.and made the ajax type as
POST
.And the ajax call as
在代码返回 JsonResult 对象之前,您需要手动读取配置部分。只需单行读取 web.config:
确保您在 web.config 中定义了配置元素
You need to read from the configuration section manually before your code returns a JsonResult object. Simply read from web.config in single line:
Be sure you defined configuration element in web.config
这对我有用
this worked for me
如果您从控制器返回视图,并且希望在 cshtml 中以 json 进行编码时增加视图包数据的长度,则可以将此代码放入 cshtml 中。
现在,
dataJsonOnActionGrid1
将可以在 js 页面上访问,您将得到正确的结果。谢谢
You can put this code in cshtml if you are returning view from controller and you want to increase the length of view bag data while encoding in json in cshtml
Now,
dataJsonOnActionGrid1
will be accesible on js page and you will get proper result.Thanks