gpt4 book ai didi

c# - 超出 JavaScriptSerializer maxJsonLength

转载 作者:行者123 更新时间:2023-11-30 19:38:20 28 4
gpt4 key购买 nike

我正在 visual studio 2013 中用 c# 创建一个网络服务。我已连接到数据库并使用以下代码返回 json。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCustomer()
{
var json = "";
var customer = from result in dc.Auto_Kada_SIA_Customers
select result;

JavaScriptSerializer jss = new JavaScriptSerializer();
jss.MaxJsonLength = Int32.MaxValue;
json = jss.Serialize(customer);
int t = json.Length;
return json;
}

但是当我尝试使用它时出现以下错误

{
"Message": "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.",
"StackTrace": " at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
"ExceptionType": "System.InvalidOperationException"
}

如果是这样我会没问题,但 MaxJsonLentgh 设置为 2,147,483,647 并且 json.Length 为 21,460,284。

问题是什么,我该如何解决?

最佳答案

尝试在 web.config 中配置最大长度,如下所示

<configuration>  
<system.web.extensions>
<scripting>
<webServices>
<!-- Update this value to set the max length -->
<jsonSerialization maxJsonLength="2147483647" />
</webServices>
</scripting>
</system.web.extensions>
</configuration>

关于c# - 超出 JavaScriptSerializer maxJsonLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33302008/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com