.net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值

在.net mvc的controller中,方法返回JsonResult,一般我们这么写:

[HttpPost]
public JsonResult QueryFeature(string url, string whereClause)
{
      string str="";
      return Json(str);
}

  此时如果str过长,就会报“使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值”

  解决方法如下:

  

[HttpPost]
public JsonResult QueryFeature(string url, string whereClause)
{
            string str="";
            
            return new JsonResult()
            {
                Data = str,
                MaxJsonLength = int.MaxValue,
                ContentType = "application/json"
            };
}

 

原文地址:https://www.cnblogs.com/tracyjfly/p/5566516.html