JavaScriptSerializer中ASP.NET MVC中的MaxJsonLength异常 [英] MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer

查看:152
本文介绍了JavaScriptSerializer中ASP.NET MVC中的MaxJsonLength异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In one of my controller actions I am returning a very large JsonResult to fill a grid.

I获取以下 InvalidOperationException 异常:

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

设置 maxJsonLength 属性在 web.config 到更高的值不幸的是没有显示任何效果。

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>

我不想像这个的答案。

在我的研究中,我发现这个博客文章写自己的 ActionResult (例如 LargeJsonResult:JsonResult

In my research I came across this blog post where writing an own ActionResult (e.g. LargeJsonResult : JsonResult) is recommended to bypass this behaviour.

这是唯一的解决方案吗?

这是ASP.NET MVC中的一个错误?

我是否缺少某些东西?

Is this then the only solution?
Is this a bug in ASP.NET MVC?
Am I missing something?

任何帮助将不胜感激。

推荐答案

似乎这已经在MVC4中修复了。

It appears this has been fixed in MVC4.

你可以这样做,这对我有用:

You can do this, which worked well for me:

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

这篇关于JavaScriptSerializer中ASP.NET MVC中的MaxJsonLength异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆