jQuery 在 MVC 4 中增加 json 响应 maxJsonLength

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15288367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:44:48  来源:igfitidea点击:

Increase json response maxJsonLength in MVC 4

jqueryajaxasp.net-mvcjson

提问by Nabeel

I am getting following error in razor view engine MVC4(.net 4.5) application when loading a large JSON response form server at

在 razor 视图引擎 MVC4(.net 4.5) 应用程序中加载大型 JSON 响应表单服务器时出现以下错误

Error during serialization or deserialization using the JSON JavaScriptSerializer.The length of the string exceeds the value set on the maxJsonLength property at @Html.Raw(Json.Encode(jsondata)”

使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了 @Html.Raw(Json.Encode(jsondata)处的 maxJsonLength 属性设置的值

I have tried by setting MaxJsonLength property in my web.config:

我已经尝试在我的 web.config 中设置 MaxJsonLength 属性:

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

Tried following at server side while sending JSON response as well.

在发送 JSON 响应的同时尝试在服务器端跟随。

 return new JsonResult()
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior,
        MaxJsonLength = Int32.MaxValue
    };

Also tried the solution listed hare: http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/. But nothing worked for me :(

还尝试了列出的解决方案:http: //brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/。但对我没有任何作用:(

Can some suggest me how to avoid this error or how to increase the Jason response max length?

有人可以建议我如何避免此错误或如何增加 Jason 响应的最大长度吗?

回答by Nabeel

Somehow I get rid of this error by using following code in view.

不知何故,我通过在视图中使用以下代码摆脱了这个错误。

@{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
}
<script type="text/javascript">
var entries = @Html.Raw(serializer.Serialize(Model.FeedEntries));    
</script>

This was not working at server side at least for me.

至少对我来说,这在服务器端不起作用。

回答by Emperor 2052

My penny to the solutions. Did b) because a) gave errormessage 'System.Web.Mvc.JsonResult does not contain a definition for maxJsonLength ... ' in Mvc 4.5 AFAIK, this is the only workaround that works.

我的一分钱的解决方案。b) 因为 a) 在 Mvc 4.5 AFAIK 中给出了错误消息“System.Web.Mvc.JsonResult 不包含 maxJsonLength ... 的定义”,这是唯一有效的解决方法。

I put b) in my controller. Hopefully this will help someone.

我把 b) 放在我的控制器中。希望这会帮助某人。

Regards, SM

问候, SM

a)

一种)

var jsonResult = Json(list, JsonRequestBehavior.AllowGet);
jsonResult.maxJsonLength = int.MaxValue;
return jsonResult;

b)

b)

if (Request.IsAjaxRequest())
{
   //Working solution
   var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };

   return new ContentResult()
   {
      Content = serializer.Serialize(list),
      ContentType = "application/json",
   };

   //Trial 2
   //var jsonResult = Json(list, JsonRequestBehavior.AllowGet);
   //jsonResult.maxJsonLength = int.MaxValue;
   //return jsonResult;

   //Trial 1
   //return Json(list, JsonRequestBehavior.AllowGet);
} 

回答by Ahamed Ishak

This worked to me

这对我有用

   return new JsonResult()
            {
                Data=jsonData,
                MaxJsonLength = 86753090,
                JsonRequestBehavior=JsonRequestBehavior.AllowGet
            };