【发布时间】:2012-07-05 20:04:30
【问题描述】:
我正在对 aspx.cs 页面中的后端 WebMethod 进行 jQuery AJAX 调用。我在 .NET JSON 序列化中遇到错误。因此,我正在寻找修复错误或避免使用 JSON(WebMethods 的唯一返回格式)的方法:
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.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary'2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)
后端代码如下(注意:result 实际上是大约 110k 的控件呈现为字符串):
[WebMethod]
public static string GetContactListControl()
{
try
{
var result = "Hello World!"
return result;
}
catch (Exception e){
Logging.LogException(e);
return "Exception Thrown";
}
}
而且我从来没有遇到过catch 块,这对我来说表明这个问题不在我的代码范围内。
我通过插入以下块找到了涉及更改 web.config 的修复程序,但它不起作用:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="123456"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
项目是 .NET 3.5。
感谢您的任何想法和建议!
【问题讨论】:
-
我假设你的意思是当你说它“永远不会命中
catch块”时,方法开头的断点永远不会被命中。使用原样的代码,我真诚地怀疑return "Hello World!"可能会抛出异常。 -
尝试将
maxJsonLength增加一吨。大约 110kb 大约是 112,640 字节,与 123,456 字节相差不远。也许额外的开销(或者“关于”不是太精确)正在打破限制。试着把它打到 9999999 看看会发生什么。 -
更正断点正常。当我在 return result 上中断时,它显示结果的值没有任何问题。
-
感谢 Chris 的建议,但使用 Web.Config 会抱怨
无效,并且搜索返回的 IIS 错误会使修复程序看起来只是 .NET 4.0。感谢 L.B.还!但由于项目限制,开源解决方案很难获得批准。 -
上面提到的IIS错误:
HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.
标签: c# json serialization .net-3.5 webmethod