Web.config jsonSerialization maxJsonLength被忽略

时间:2013-12-11 15:41:04

标签: vb.net asp.net-mvc-3 webclient javascriptserializer

我在MVC3下运行了.NET 4.0个应用程序,当我使用JavascriptSerializer.Deserialize时,我收到错误。

  

使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。

阅读Can I set an unlimited length for maxJsonLength in web.config我将jsonSerialization maxJsonLength密钥放在我的web.config中,但它被忽略了。虽然我可以在代码中设置JavaScriptSerializer.MaxJsonLength属性,但它可以正常工作。

我想在Web.config而不是代码中获得值。以下是我使用JavaScriptSerializer的方式。

Dim client As New WebClient
...
Dim result = _client.DownloadString("Test")
Dim serializer = New JavaScriptSerializer
Return serializer.Deserialize(Of Foo)(result)

这是我的Web.config

<configuration>
    <configSections>
    ...
    </configSections>
    <appSettings>
    ...
    </appSettings>
    <system.web>
    <customErrors mode="On">
      <error statusCode="404" redirect="/Http404"/>
      <error statusCode="403" redirect="/Http403"/>
      <error statusCode="550" redirect="/Http550"/>
    </customErrors>
    <compilation debug="true" targetFramework="4.0"/>
    <pages controlRenderingCompatibilityVersion="4.0">
      <namespaces>
        <add namespace="System.Web.Helpers"/>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Web.WebPages"/>
      </namespaces>
    </pages>
  </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
    </scripting>
    </system.web.extensions>
 <system.webServer>
 ....
</system.webServer>
</configuration>

1 个答案:

答案 0 :(得分:10)

根据您提供的链接(排名第二的答案),您的web.config设置将被忽略,因为您使用的是JavaScriptSerializer内部实例

如果您需要将值存储在web.config中,则可以在名为<appSettings>的{​​{1}}部分中添加一个值为maxJsonLength的密钥,然后在您的代码中,你可以使用它:

50000000
相关问题