【问题标题】:JSON maxJsonLength PropertyJSON maxJsonLength 属性
【发布时间】:2018-09-11 11:52:28
【问题描述】:

我在 .cshtml 文件中使用以下代码。

var typeVM = JSON.parse('@Html.Raw(Json.Encode(Model.typeVM))');

显示以下错误: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

我无法为Json Encode 函数定义最大长度,并且Web.config 配置仅适用于web-services

那么问题是这个问题的可能解决方案是什么?
如何定义jsonMaxLength 属性?

【问题讨论】:

  • 首先,它应该是var typeVM = @Html.Raw(Json.Encode(Model.typeVM))(将您的 json 转换为字符串然后再将其转换回来毫无意义)。要问自己的真正问题是,为什么首先要向视图发送这么多数据
  • 重点是我在直接将字符串转换为 json 对象时遇到了转义序列问题。

标签: c# json asp.net-mvc asp.net-mvc-4 jsonserializer


【解决方案1】:

我假设它是您从中获取数据的网络服务(因为您的问题被标记为“网络服务”),请在 web.config 中更改 maxlength:

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

【讨论】:

  • 感谢您的帮助,但它不是网络服务。我已经试过了,它不能与 MVC 一起工作
  • @Sameer 也适用于 mvc,尝试将该配置添加到您的 web.config 文件中并告知它是否有效。
  • 实际上,有时会发生什么。在模型中具有较大的属性名称或带有下划线的模型属性,这样 json 对话无法正常工作,请尽可能修改您的模型,然后再次尝试您向我们突出显示的上述语句。谢谢。
  • 有没有办法为整个项目定义 maxjsonlength 属性?
  • 定义maxjsonlength有3种方式 1-in IIS, 2-Web.config 3- return new JsonResult() { Data = data, JsonRequestBehavior = behavior, MaxJsonLength = Int32.MaxValue };
【解决方案2】:
Model variable must be short 
for example 
model.First_name it should be short to model.FN

我希望它对你有用..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多