c# - 如何在不更改 maxJsonLength 的情况下发送大型/多部分 JSON 对象?

标签 c# ajax json webmethod windows-server

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

InvalidOperationException: The length of the string exceeds the value set on the maxJsonLength property.

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

最佳答案

这不是一个无需编辑 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>

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

关于c# - 如何在不更改 maxJsonLength 的情况下发送大型/多部分 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002035/

相关文章:

javascript - Rails 如何通过 :update controller action 中的 Ajax 调用返回 javascript

javascript - C# Web 方法未在 javascript 中调用

Java 7 解析日期时间,秒数为小数点后 7 位

c# - 在 C# 中通过 Imgur API 匿名获取图像信息

c# - 如何使用 JSON.NET 反序列化并保留父对象

c# - 我如何向 API 发出 XML 或 JSON 请求

c# - CaSTLe DynamicProxy IInterceptor 或 ProxyGenerator 应该缓存吗?

javascript - 使用 AJAX 使用从 mysql 数据库中提取的数据填充表单

javascript - jQuery ajax 不显示和隐藏加载器 div

c# - 如何处理paypal电子支票