Web.config中jsonSerialization maxJsonLength忽略 [英] Web.config jsonSerialization maxJsonLength ignored

查看:1183
本文介绍了Web.config中jsonSerialization maxJsonLength忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET 4.0 键,当我使用的JavaScriptSerializer运行的 MVC3 应用程序.Deserialize 我得到一个错误。


  

使用JSON序列化的JavaScriptSerializer反序列化或过程中的错误。字符串的长度超过上maxJsonLength属性设置的值。


阅读<一href=\"http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config\">Can我在web.config中设置无限长度maxJsonLength 我把 jsonSerialization maxJsonLength 键在我的web.config,但它被忽略。虽然我可以设置code中的 JavaScriptSerializer.MaxJsonLength 属性,它工作正常。

我想有在的Web.config ,而不是code中的价值。这里是我如何使用的JavaScriptSerializer

 昏暗的客户端作为新的Web客户端
...
昏暗的结果= _client.DownloadString(测试)
昏暗的串行器=新的JavaScriptSerializer
返回serializer.Deserialize(中富)(结果)

下面是我的的Web.config

 &LT;结构&gt;
    &LT; configSections&GT;
    ...
    &LT; / configSections&GT;
    &LT;&的appSettings GT;
    ...
    &LT; /的appSettings&GT;
    &LT;&的System.Web GT;
    &LT;的customErrors模式=ON&GT;
      &LT;错误状态code =404重定向=/ HTTP404/&GT;
      &LT;错误状态code =403重定向=/ Http403/&GT;
      &LT;错误状态code =550重定向=/ Http550/&GT;
    &LT; /&的customErrors GT;
    &LT;编译调试=真targetFramework =4.0/&GT;
    &LT;页面controlRenderingCompatibilityVersion =4.0&GT;
      &LT;&命名空间GT;
        &LT;添加命名空间=System.Web.Helpers/&GT;
        &LT;添加命名空间=System.Web.Mvc/&GT;
        &LT;添加命名空间=System.Web.Mvc.Ajax/&GT;
        &LT;添加命名空间=System.Web.Mvc.Html/&GT;
        &LT;添加命名空间=System.Web.Routing/&GT;
        &LT;添加命名空间=System.Web.WebPages/&GT;
      &LT; /命名空间&GT;
    &LT; /页&GT;
  &LT; /system.web>
  &LT; System.Web.Extensions程序&GT;
    &LT;脚本&GT;
      &LT;&Web服务GT;
        &LT; jsonSerialization maxJsonLength =5000/&GT;
      &LT; / WebServices的&GT;
    &LT; /脚本&GT;
    &LT; /system.web.extensions>
 &LT; system.webServer&GT;
 ....
&LT; /system.webServer>
&LT; /结构&gt;


解决方案

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

如果你需要存储在web.config中的值,可以添加在℃的关键;的appSettings&GT; 部分名为 maxJsonLength 与值 5000 ,然后在code,你可以使用它像:

  VAR串=新的JavaScriptSerializer();
serializer.MaxJsonLength = ConfigurationManager.AppSettings ['maxJsonLength'];

I have an MVC3 application running under .NET 4.0 and when I use JavascriptSerializer.Deserialize I'm getting an error.

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

Reading Can I set an unlimited length for maxJsonLength in web.config I put the jsonSerialization maxJsonLength key in my web.config but it gets ignored. Though I can set the JavaScriptSerializer.MaxJsonLength property in code and it works fine.

I would like to have the value in the Web.config rather than code. Here is how I am using JavaScriptSerializer.

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

Here is my 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>

解决方案

According to the link you provided (the 2nd most voted answer), your web.config setting will be ignored because you're using an internal instance of the JavaScriptSerializer.

If you need the value to be stored in the web.config, you could add a key in the <appSettings> section called maxJsonLength with a value of 50000000 and then in your code, you could use it like:

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

这篇关于Web.config中jsonSerialization maxJsonLength忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆