WebMethod - JSON序列化错误

时间:2012-07-05 20:04:31

标签: c# json serialization .net-3.5 webmethod

我正在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>

Project is .NET 3.5。

感谢您的任何想法和建议!

1 个答案:

答案 0 :(得分:2)

必须在web.config中设置maxJsonLength属性的配置。为了允许IIS允许此配置,<sectionGroup>中必须包含以下<configSections>

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
      <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    </sectionGroup>
  </sectionGroup>
</sectionGroup>