【问题标题】:Web.config jsonSerialization maxJsonLength ignoredWeb.config jsonSerialization maxJsonLength 被忽略
【发布时间】:2013-12-29 16:18:31
【问题描述】:

我有一个在.NET 4.0 下运行的MVC3 应用程序,当我使用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>

【问题讨论】:

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


    【解决方案1】:

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

    如果您需要将值存储在 web.config 中,您可以在名为 maxJsonLength&lt;appSettings&gt; 部分中添加一个值为 50000000 的键,然后在您的代码中,您可以像这样使用它:

    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = ConfigurationManager.AppSettings['maxJsonLength'];
    

    【讨论】:

    • 你应该加粗内部实例。那是我看错了!
    • 完成。也刚刚意识到我给了你一个 C# 示例,直到现在才看到你的 vb.net 标签:)
    猜你喜欢
    • 2014-02-25
    • 2012-09-26
    • 2010-09-22
    • 2011-10-12
    • 2011-04-27
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多