[ASP.NET MVC] JsonResult返回內容太大, 發生 InvalidOperationException

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

Code

可以直接指定 JsonResult 的 MaxJsonLength, 預設為 2097152

public async Task<JsonResult> Get()
{
    JsonResult result = Json(await GetData(), JsonRequestBehavior.AllowGet);
    result.MaxJsonLength = 5_000_000;
    return result;
}

參考資料

JavaScriptSerializer.MaxJsonLength Property