Reputation: 3
I'm getting the following exception when returning a large JSON result:
The length of the string exceeds the value set on the maxJsonLength property.
I am trying to catch the exception where the serialisation happens but this does not happen.
try
{
return new JsonResult()
{
Data = LargeJsonData,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
catch (Exception)
{
return "Error";
}
I do NOT want the adjust the property. I just want to catch the exception and handle appropriately.
I have found the following question which seems like a duplicate, but the accepted answer modifies the maxJsonLength property.
How to catch error during serialization or deserialization using the JSON JavaScriptSerializer
Thanks, Steve
Upvotes: 0
Views: 487
Reputation: 14672
This will be being thrown when the ExecuteContext method is called on the Json result. You could work around this by defining your own override for JsonResult and catching it here, e.g.
public class CustomJsonResult : JsonResult
{
private const string _dateFormat = "yyyy-MM-dd HH:mm:ss";
public override void ExecuteResult(ControllerContext context)
{
try
{
base.ExecuteResult(context);
}
catch(Exception ex)
{
//Handle it
}
}
And then returning an instance of this instead of the JsonResult.
Upvotes: 1