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

时间:2023-03-08 17:38:14

在.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"
};
}