2012-07-05 47 views
0

我正在對aspx.cs頁面中的後端WebMethod進行jQuery AJAX調用。我在.NET JSON序列化中遇到錯誤。因此,我想辦法要麼修復錯誤或避免使用JSON(爲的WebMethods只返回格式):WebMethod - JSON序列化錯誤

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

相關的堆棧跟蹤是: 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。

謝謝你的任何想法和建議!

+0

我假設你意味着,在方法的開頭設置一個斷點,當你說這是從來沒有擊中它「永遠不會碰到」catch「塊」。用代碼,我真誠地懷疑'返回'Hello World!''可能會拋出異常。 – 2012-07-05 20:06:55

+0

嘗試增加'maxJsonLength'多一噸。大約110kb大約是112,640字節,與123,456字節不太相近。也許額外的開銷(或「大約」不是太準確)正在突破限制。嘗試衝壓到9999999,看看會發生什麼。 – 2012-07-05 20:09:03

+0

正確的斷點很好。當我在_return_ _result_上打破時,它顯示結果的值沒有任何問題。 – WhistlingZebra 2012-07-05 20:10:54

回答

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>