Json Encode Exceeding Exceptional Json Length

I am trying to send json from my MVC controller, its exceptions exceptions, errors during serialization or deserialization using JSON JavaScriptSerializer. The string is longer than the value specified for the maxJsonLength property.

I googled and added the maximum length in my configuration, also redefined my json method, nothing worked.

Here is my web configuration and my method, its throwing exception. in appsetting

<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" /> <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="2147483647"> </jsonSerialization> </scripting> </system.web.extensions> 

over ridden method

  protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior) { return new JsonResult() { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior, MaxJsonLength = Int32.MaxValue }; } 

my method

  public JsonResult GetAttributeControls() { List<SelectListItem> attrControls; using (var context = new Context()) { attrControls = context.AttributeControls.ToList(). Select(ac => new SelectListItem { Text = ac.Name, Value = ac.AttributeControlId.ToString() }).ToList(); } //var jsonResult = Json(attrControls, JsonRequestBehavior.AllowGet); //jsonResult.MaxJsonLength = int.MaxValue; //return jsonResult; return Json(attrControls,JsonRequestBehavior.AllowGet); } 

I get an exception in the next line this is my load.chtml file

 <script type="text/javascript"> var InitialRowData = '@Html.Raw(Json.Encode(Model))'; var isLayoutEditable = false; var occupied = '@Model.occupied'; var notoccupied = '@Model.notoccupied'; var blocked = '@Model.blocked'; </script> 

@ Html.Raw (Json.Encode (model));

there the maximum json length is about 200000, how to increase the size, nothing happens. any help please?

Thanks in advance.

+7
source share
3 answers

Ok, so I just recently had the same issue. I tried to do @Html.Raw(Json.Encode(Model)) to send the model to javascript and received an error message that the string was too long.

I made my way for the answer to this question and could not find anything that directly answered the question, however I found this answer in the stack , which I then used to solve our problem.

The idea here is to configure the JavaScriptSerializer , manually set MaxJsonLength , serialize the model, and then pass it to javascript.

In the HTML of your view:

 @{ var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; var jsonModel = serializer.Serialize(Model); } 

In your javascript:

 <script> $(function () { var viewModel = @Html.Raw(jsonModel); // now you can access Model in JSON form from javascript }); </script> 
+19
source

try the steps on this link. Basically, what happened because the json object's return size is too large and exceeds the default maximum message size size

http://www.smartdigitizers.com/net-development-tips/error-during-serialization-or-deserialization-using-the-json-javascriptserializer/

+1
source

I recently ran into the same problem and changed my serialized code and it works as expected. Use the code below in your code: var InitialRowData = '@ Html.Raw (JsonConvert.SerializeObject (Model))';

0
source

Source: https://habr.com/ru/post/970670/


All Articles