本文介绍了增加JSON响应maxJsonLength在MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在

使用字符串的JSON JavaScriptSerializer.The长度序列化和反序列化过程中的错误超出在@ Html.Raw(Json.En code在maxJsonLength属性设置的值(jsondata

我已经通过设置MaxJsonLength财产在我的web.config尝试:

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

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

试了下,在服务器端在发送JSON响应为好。

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

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

还试图上市的解决方案野兔:http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/.但没有为我工作:(

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 :(

能否有人建议我如何避免这种错误,或者如何提高贾森回应最大长度?

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

推荐答案

我一分钱的解决方案。难道B),因为a)得到errormessage的MVC中System.Web.Mvc.JsonResult不包含maxJsonLength的定义......4.5 AFAIK,这是工作的唯一的解决方法。

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)。希望这将帮助别人。

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

问候,SM

一)

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

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);
} 

这篇关于增加JSON响应maxJsonLength在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:05