我正试图在我的控制器中POST一个Json字符串,该字符串包含9K行,我正在使用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>
我推荐你Json.NET或Jil
Microsoft Docs 中的更多文档
如果您手动使用JavascriptSerializer,web.config将无效。您必须在创建的实例中设置maxJsonLength:
var js = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
如果什么都不起作用,也许你可以使用自己的ValueProviderFactory:
https://translate.google.es/translate?hl=es&sl=自动&tl=en&u=https%3A%2F%2Blog.naver.com%2Ftechshare%2F100145191355