Asp.net-Mvc

JavaScriptSerializer 期間 ASP.NET MVC 中的 MaxJsonLength 異常

  • April 17, 2011

在我的一個控制器操作中,我返回一個非常大JsonResult的來填充網格。

我收到以下InvalidOperationException異常:

使用 JSON JavaScriptSerializer 進行序列化或反序列化時出錯。字元串的長度超過了 maxJsonLength 屬性上設置的值。

maxJsonLength不幸的是,將 中的屬性設置web.config為更高的值不會顯示任何效果。

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

我不想將它作為這個SO 答案中提到的字元串傳回。

在我的研究中,我遇到了這篇部落格文章,其中建議編寫自己的ActionResult(例如LargeJsonResult : JsonResult)來繞過這種行為。

這是唯一的解決方案嗎?

這是 ASP.NET MVC 中的錯誤嗎?

我錯過了什麼嗎?

非常感激任何的幫助。

看來這已在 MVC4 中修復。

你可以這樣做,這對我很有效:

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

引用自:https://stackoverflow.com/questions/5692836