获取 maxJsonLength 属性 错误

Getting maxJsonLength property error

获取错误:

An exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll but was not handled in user code

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

在我的应用程序设置文件中添加了:

<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />

并且在我的 webconfig 文件中添加了:

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

仍然出现错误。

"return" 中出现错误,这是我的 C# 代码:

  public String GetBulkContacts(string KeyName, string GroupName)
        {
            var result = _bulkMailService.GetBulkMailContacts(KeyName, GroupName);
            return new JavaScriptSerializer().Serialize(result);
        }

你可以改成这个;

var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
return serializer.Serialize(result);

据我所知,web.config 设置仅适用于 MVC 内部序列化器,不适用于您自己实例化的序列化器...

您也可以使用;

return new JsonResult()
    {
        Data = result,
        MaxJsonLength = Int32.MaxValue
    };

相反。另外我认为你应该使用

JsonConvert.SerializeObject(result);

最近不再使用直接的 JavaScriptSerializer。