Can I set an unlimited length for maxJsonLength in web.config?

后端 未结 29 3160
礼貌的吻别
礼貌的吻别 2020-11-21 06:43

I am using the autocomplete feature of jQuery. When I try to retrieve the list of more then 17000 records (each won\'t have more than 10 char length), it\'s exceeding the le

相关标签:
29条回答
  • 2020-11-21 07:26

    Simply set MaxJsonLength proprty in MVC's Action method

    JsonResult json= Json(classObject, JsonRequestBehavior.AllowGet);
    json.MaxJsonLength = int.MaxValue;
    return json;
    
    0 讨论(0)
  • 2020-11-21 07:26

    If you are encountering this sort of issue in View, you can use below method to resolve that. Here Iused Newtonsoft package .

    @using Newtonsoft.Json
    <script type="text/javascript">
        var partData = @Html.Raw(JsonConvert.SerializeObject(ViewBag.Part));
    </script>
    
    0 讨论(0)
  • 2020-11-21 07:26

    use lib\Newtonsoft.Json.dll

    public string serializeObj(dynamic json) {        
        return JsonConvert.SerializeObject(json);
    }
    
    0 讨论(0)
  • 2020-11-21 07:27
     JsonResult result = Json(r);
     result.MaxJsonLength = Int32.MaxValue;
     result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
     return result;
    
    0 讨论(0)
  • 2020-11-21 07:28

    I solved the problem adding this code:

    String confString = HttpContext.Current.Request.ApplicationPath.ToString();
    Configuration conf = WebConfigurationManager.OpenWebConfiguration(confString);
    ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)conf.GetSection("system.web.extensions/scripting/webServices/jsonSerialization");
    section.MaxJsonLength = 6553600;
    conf.Save();
    
    0 讨论(0)
  • 2020-11-21 07:30

    NOTE: this answer applies only to Web services, if you are returning JSON from a Controller method, make sure you read this SO answer below as well: https://stackoverflow.com/a/7207539/1246870


    The MaxJsonLength property cannot be unlimited, is an integer property that defaults to 102400 (100k).

    You can set the MaxJsonLength property on your web.config:

    <configuration> 
       <system.web.extensions>
           <scripting>
               <webServices>
                   <jsonSerialization maxJsonLength="50000000"/>
               </webServices>
           </scripting>
       </system.web.extensions>
    </configuration> 
    
    0 讨论(0)
提交回复
热议问题