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

后端 未结 29 3253
礼貌的吻别
礼貌的吻别 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:37

    Just ran into this. I'm getting over 6,000 records. Just decided I'd just do some paging. As in, I accept a page number in my MVC JsonResult endpoint, which is defaulted to 0 so it's not necessary, like so:

    public JsonResult MyObjects(int pageNumber = 0)
    

    Then instead of saying:

    return Json(_repository.MyObjects.ToList(), JsonRequestBehavior.AllowGet);
    

    I say:

    return Json(_repository.MyObjects.OrderBy(obj => obj.ID).Skip(1000 * pageNumber).Take(1000).ToList(), JsonRequestBehavior.AllowGet);
    

    It's very simple. Then, in JavaScript, instead of this:

    function myAJAXCallback(items) {
        // Do stuff here
    }
    

    I instead say:

    var pageNumber = 0;
    function myAJAXCallback(items) {
        if(items.length == 1000)
            // Call same endpoint but add this to the end: '?pageNumber=' + ++pageNumber
        }
        // Do stuff here
    }
    

    And append your records to whatever you were doing with them in the first place. Or just wait until all the calls finish and cobble the results together.

提交回复
热议问题