加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – Web.config jsonSerialization maxJsonLength被忽略

发布时间:2020-12-17 00:07:35 所属栏目:大数据 来源:网络整理
导读:我有一个在.NET 4.0下运行的MVC3应用程序,当我使用 JavascriptSerializer.Deserialize时,我收到一个错误. Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the max
我有一个在.NET 4.0下运行的MVC3应用程序,当我使用 JavascriptSerializer.Deserialize时,我收到一个错误.

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

阅读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>
根据您提供的链接(排名第二的答案),您的web.config设置将被忽略,因为您正在使用JavaScriptSerializer的内部实例.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读