Thursday, May 2, 2024

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

 public ActionResult GetDocumentCompData(string docId)

         {

             SessionValues sessionValues = new GetSessionValues();

             try

             {

                 ResponseModel response = ApiCalls.Get(GetApiUrls.GetDocumentInfo + "?documentId=" + docId);


                 if (response != null && response.Data != null)

                 {

                     DocumentsModel retval = JsonConvert.DeserializeObject<DocumentsModel>(response.Data.ToString());

                     retval.FileName = Path.GetFileName(retval.FileLocation);


                     // Serialize the DocumentsModel object to JSON and stream it directly to the response

                     var serializer = JsonSerializer.CreateDefault();

                     Response.BufferOutput = false; // Disable buffering to stream content

                     Response.ContentType = "application/json";


                     using (var streamWriter = new StreamWriter(Response.OutputStream))

                     using (var jsonWriter = new JsonTextWriter(streamWriter))

                     {

                         serializer.Serialize(jsonWriter, new { Data = retval, Message = response.Message, Status = response.Status });

                     }


                     // Ensure the response is complete

                     Response.Flush();

                     return new EmptyResult(); // Return an EmptyResult to indicate that the action has completed

                 }

             }

             catch (Exception e)

             {

                 bool rethrow = CommonException.HandleException(e, Enums_Constants.ControllerPolicy);

                 if (rethrow)

                     throw;

             }

             return Json("", JsonRequestBehavior.AllowGet);

         }