首页 > 解决方案 > 如何解决 ASP.NET MVC 3 中 javaScriptSerializer 中超过 maxJsonLength 的异常?

问题描述

我正在尝试在我的控制器中发布一个包含 9K 行的 Json 字符串,并且我正在使用 TryUpdateModel 使用 Json 字符串更新模型,但收到此错误:

InvalidOperationException 异常:使用 JSON JavaScriptSerializer 进行序列化或反序列化期间出错。字符串长度超过值

我正在使用 MVC 3。我尝试在我的 web.config 文件中配置 json 请求的最大长度,但仍然无法正常工作。有人可以帮忙吗?

标签: .netmodel-view-controller

解决方案


尝试在 web.config 中为 JavaScriptSerializer 设置 maxJsonLength,检查以下细微差别:

  • maxJsonLength 表示字符串中 UTF-8 字符的最大数量。
  • maxJsonLength 是 int32 所以最大值是 +2.147.483.647

即:(根据需要更改程序集版本和 PublicKeyToken)

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

    <configuration>
      <system.web.extensions>
        <scripting>
          <webServices>
            <jsonSerialization maxJsonLength="5000"/> 
          </webServices>
        </scripting>
      </system.web.extensions>
    </configuration>

您还可以使用自定义转换器:

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50"/>
          <converters>
            <add name="MyCustomConverter" 
              type="MyCompany.ConvertersNameSpace.MyTypeConverter"/>
          </converters>
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

我推荐你Json.NETJil

Microsoft Docs中的更多文档

如果您手动使用 JavascriptSerializer,则 web.config 无效。您必须将 maxJsonLength 设置为您创建的实例:

var js = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };

如果没有任何效果,也许您可​​以使用自己的 ValueProviderFactory:

https://translate.google.es/translate?hl=es&sl=auto&tl=en&u=https%3A%2F%2Fblog.naver.com%2Ftechshare%2F100145191355


推荐阅读