【问题标题】:The length of the string exceeds the value set on the maxJsonLength property. in MVC3字符串的长度超过了 maxJsonLength 属性上设置的值。在 MVC3 中
【发布时间】:2013-10-07 04:09:57
【问题描述】:

MVC3(.cshtml 文件)

$.getJSON(URL, Data, function (data) {

                    document.getElementById('divDisplayMap').innerHTML = data;

                    if (data != null) {
                        $('#myTablesId').show();
                        tdOnclickEvent();

                    }
                    else {
                        $('#myTablesId').hide();
                    }
                }).error(function (xhr, ajaxOptions, thrownError) { debugger; });

在服务器端

 public JsonResult ZoneType_SelectedState(int x_Id, int y_Id)
    {
    JsonResult result = new JsonResult();
     result.Data = "LongString";//Longstring with the length mention below
    return Json(result.Data,"application/json", JsonRequestBehavior.AllowGet);
    }

从服务器端,我传递的字符串长度为 1194812 及更多。 但我收到错误消息说

"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

请帮我搞定ASP

【问题讨论】:

    标签: json asp.net-mvc-3 jquery jsonresult


    【解决方案1】:

    您可以编写一个自定义的 ActionResult,它允许您指定序列化程序可以处理的最大数据长度:

    public class MyJsonResult : ActionResult
    {
        private readonly object data;
        public MyJsonResult(object data)
        {
            this.data = data;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.RequestContext.HttpContext.Response;
            response.ContentType = "application/json";
            var serializer = new JavaScriptSerializer();
            // You could set the MaxJsonLength to the desired size - 10MB in this example
            serializer.MaxJsonLength = 10 * 1024 * 1024;
            response.Write(serializer.Serialize(this.data));
        }
    }
    

    然后使用它:

    public ActionResult ZoneType_SelectedState(int x_Id, int y_Id)
    {
        string data = "LongString";//Longstring with the length mention below;
        return new MyJsonResult(data);
    }
    

    【讨论】:

      【解决方案2】:

      尝试更新您的Controller 方法,如下所示:

      public JsonResult ZoneType_SelectedState(int x_Id, int y_Id)
      {
          var result = Json("LongString", JsonRequestBehavior.AllowGet);
          result.MaxJsonLength = int.MaxValue;
          return result;
      }
      

      希望这会有所帮助...

      【讨论】:

        猜你喜欢
        • 2015-03-27
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 2015-05-30
        • 2018-04-25
        • 2017-08-31
        • 2015-04-06
        • 2013-05-26
        相关资源
        最近更新 更多