【问题标题】:How to send large/multi-part JSON object without changing maxJsonLength?如何在不更改 maxJsonLength 的情况下发送大/多部分 JSON 对象?
【发布时间】:2014-05-24 23:39:46
【问题描述】:

在我当前的 C# Web 应用程序中,我从 WebMethod 请求一个 JSON 对象,这个 JSON 对象超过了 maximumJSONLength。结果:

InvalidOperationException: 字符串长度超过 maxJsonLength 属性设置的值。

我当前的解决方案是将maxJsonlength(在 Web.config 中)提高到对象的 JSON 长度之上,但现在我正在将我的应用程序迁移到 Windows Server(这似乎不支持此覆盖)。有没有办法通过 ajax POST 部分发送 JSON 对象?如果是这样,最好的解决方案是什么,我已经寻找一种方法来分多个部分发送数据,但没有明确的解决方案。

【问题讨论】:

  • 我知道的唯一方法是更改​​实际的 API - 例如添加一个“has more”(也称为“offset”)键,客户端可以使用该键为同一源查询请求其他结果。
  • 只是在这里跳出框框思考,但您可以将其下载为 JSON 文件然后解析它?

标签: c# ajax json webmethod windows-server


【解决方案1】:

这不是一个不编辑 maxJsonLength 的解决方案,但它允许在 Windows Server 中修改 maxJsonLength。

将以下内容添加到我的 web.config 允许我随后编辑 maxJsonLength。

<configuration>
  <configSections>
    <sectionGroup name="system.web.extensions"         type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
            <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
    </sectionGroup>
    </sectionGroup>
  </configSections>
  ... 
</configuration>

我希望这可以帮助其他遇到类似问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 2022-11-30
    • 1970-01-01
    • 2021-08-06
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多