zac*_*ack 5 vb.net webclient javascriptserializer asp.net-mvc-3
我有一个MVC3运行的应用程序.NET 4.0,当我使用时,JavascriptSerializer.Deserialize我收到一个错误.
使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过maxJsonLength属性上设置的值.
阅读我可以在web.config中为maxJsonLength设置无限长度我把jsonSerialization maxJsonLength密钥放在我的web.config中但它被忽略了.虽然我可以JavaScriptSerializer.MaxJsonLength在代码中设置属性,但它工作正常.
我想在Web.config代码而不是代码中获得价值.这是我如何使用JavaScriptSerializer.
Dim client As New WebClient
...
Dim result = _client.DownloadString("Test")
Dim serializer = New JavaScriptSerializer
Return serializer.Deserialize(Of Foo)(result)
Run Code Online (Sandbox Code Playgroud)
这是我的 Web.config
<configuration>
<configSections>
...
</configSections>
<appSettings>
...
</appSettings>
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="/Http404"/>
<error statusCode="403" redirect="/Http403"/>
<error statusCode="550" redirect="/Http550"/>
</customErrors>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="4.0">
<namespaces>
<add namespace="System.Web.Helpers"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
<system.webServer>
....
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
lha*_*han 10
根据您提供的链接(2最投票的答案),你的web.config设置会因为你使用的是被忽略内部实例的JavaScriptSerializer.
如果您需要将值存储在web.config中,您可以在<appSettings>调用maxJsonLength的值部分中添加一个键50000000,然后在代码中,您可以使用它:
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = ConfigurationManager.AppSettings['maxJsonLength'];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11384 次 |
| 最近记录: |