The length of the string exceeds the value set on the maxJsonLength property. in MVC3

后端 未结 2 1488
我寻月下人不归
我寻月下人不归 2021-01-26 15:29

MVC3 (.cshtml File)

$.getJSON(URL, Data, function (data) {

                    document.getElementById(\'divDisplayMap\').innerHTML = data;

                            


        
2条回答
  •  野性不改
    2021-01-26 16:15

    You could write a custom ActionResult which will allow you to specify the maximum length of data that the serializer can handle:

    public class MyJsonResult : ActionResult
    {
        private readonly object data;
        public MyJsonResult(object data)
        {
            this.data = data;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.RequestContext.HttpContext.Response;
            response.ContentType = "application/json";
            var serializer = new JavaScriptSerializer();
            // You could set the MaxJsonLength to the desired size - 10MB in this example
            serializer.MaxJsonLength = 10 * 1024 * 1024;
            response.Write(serializer.Serialize(this.data));
        }
    }
    

    and then use it:

    public ActionResult ZoneType_SelectedState(int x_Id, int y_Id)
    {
        string data = "LongString";//Longstring with the length mention below;
        return new MyJsonResult(data);
    }
    

提交回复
热议问题