.net - 如何解决 ASP.NET MVC 3 中 javaScriptSerializer 中超过 maxJsonLength 的异常?
问题描述
我正在尝试在我的控制器中发布一个包含 9K 行的 Json 字符串,并且我正在使用 TryUpdateModel 使用 Json 字符串更新模型,但收到此错误:
InvalidOperationException 异常:使用 JSON JavaScriptSerializer 进行序列化或反序列化期间出错。字符串长度超过值
我正在使用 MVC 3。我尝试在我的 web.config 文件中配置 json 请求的最大长度,但仍然无法正常工作。有人可以帮忙吗?
解决方案
尝试在 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>
Microsoft Docs中的更多文档
如果您手动使用 JavascriptSerializer,则 web.config 无效。您必须将 maxJsonLength 设置为您创建的实例:
var js = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
如果没有任何效果,也许您可以使用自己的 ValueProviderFactory:
推荐阅读
- c++ - 不能直接将数字转换为以空字符结尾的十六进制字符串,必须转换为 std::string 然后使用 .c_str()
- c# - 如何使用 FLUENT API 配置这种模型关系
- matplotlib - 如何可视化月度数据?
- postgresql - PostgreSQL 使用 CurrentDatabase 设置变量
- forms - 如何在 pwsh 中使用新的 param -form 登录网站
- php - 注入
使用 DOMDocument 在文本内 - testng - testng-users › 如何在 TestNG 中将测试用例标记为不适用,不想跳过或忽略测试用例
- python - 从 Python 中的句子中删除单词而不是子词
- c# - 编写函数(方法)打印出交易明细
- angular - Angular 7:如果存在值,则在 addControl 上触发值更改