I am getting the error "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."
when trying to convert a MVC partial view to string when the payload exceeds the MaxJsonLength
property.
How do I go about setting the MaxJsonLength property in this case? I've tried setting the <jsonSerialization maxJsonLength="2147483644"/>
property in the web.config, as per this post, but that doesn't have any effect. I am not entirely where to go from here and looking for a bit of a guidance.
The code errors on the viewResult.View.Render(viewContext, sw);
line below:
protected string ConvertViewToString(string viewName, object model)
{
string razorView = string.Empty;
if (string.IsNullOrWhiteSpace(viewName)) return razorView;
if (model != null && ViewData != null)
{
ViewData.Model = model;
}
if (ControllerContext != null)
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
if (viewResult != null)
{
using (StringWriter sw = new StringWriter())
{
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
razorView = sw.GetStringBuilder().ToString();
}
}
}
return razorView;
}