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

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

    How about some attribute magic?

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class MaxJsonSizeAttribute : ActionFilterAttribute
    {
        // Default: 10 MB worth of one byte chars
        private int maxLength = 10 * 1024 * 1024;
    
        public int MaxLength
        {
            set
            {
                if (value < 0) throw new ArgumentOutOfRangeException("value", "Value must be at least 0.");
    
                maxLength = value;
            }
            get { return maxLength; }
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            JsonResult json = filterContext.Result as JsonResult;
            if (json != null)
            {
                if (maxLength == 0)
                {
                    json.MaxJsonLength = int.MaxValue;
                }
                else
                {
                    json.MaxJsonLength = maxLength;
                }
            }
        }
    }
    

    Then you could either apply it globally using the global filter configuration or controller/action-wise.

提交回复
热议问题