我正在使用visual studio 2013中的c#创建web服务。我连接到一个数据库,并使用以下代码返回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。
问题出在哪里?我该如何解决?
发布于 2015-10-23 20:32:17
尝试在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> https://stackoverflow.com/questions/33302008
复制相似问题