MVC 中的 LargeJsonResult 返回许多值

本文关键字:许多值 返回 LargeJsonResult 中的 MVC | 更新日期: 2023-09-27 18:32:15

我有一个返回许多数据的控制器。 然后我得到了"在使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了在 maxJsonLength 属性上设置的值。

我已经用这个添加了我的 web.config。但错误仍然发生。

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483645" recursionLimit="100">
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>

然后我添加新类 LargeJsonResult 就像在这个网站上一样 http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/

它在控制器中是这样说的

return new LargeJsonResult() { Data = output, MaxJsonLength = int.MaxValue };

但是我怎样才能将它与许多返回数据一起使用呢? 下面是我的控制器

public ActionResult LoadInitData()
        {
            try
            {
                Database db = new Database("CON001");
                _employee = Helper.Common.GetEmployeeData(db);
                EmployeeDAC dacEmployee = new EmployeeDAC(db);
                Employee dataEmployee = dacEmployee.GetDataByComputerLogin(GetUser());
                if (_employee.SBU == "FB")
                {
                    BrandBudgetDAC dacBrandBudget = new BrandBudgetDAC(db);
                    List<BrandBudget> dataBrandBudget = dacBrandBudget.GetDataBrandFB();
                    PostBudgetDAC dacPostBudget = new PostBudgetDAC(db);
                    List<PostBudget> dataPostBudget = dacPostBudget.GetDataPostFB();
                    AreaDAC dacArea = new AreaDAC(db);
                    List<Area> dataArea = dacArea.GetData();
                    return Json(new { Employee = dataEmployee, BrandBudget = dataBrandBudget, PostBudget = dataPostBudget, Area = dataArea }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    BrandBudgetDAC dacBrandBudget = new BrandBudgetDAC(db);
                    List<BrandBudget> dataBrandBudget = dacBrandBudget.GetData(_employee.SBU);
                    PostBudgetDAC dacPostBudget = new PostBudgetDAC(db);
                    List<PostBudget> dataPostBudget = dacPostBudget.GetData(_employee.SBU);
                    AreaDAC dacArea = new AreaDAC(db);
                    List<Area> dataArea = dacArea.GetData();
                    return Json(new { Employee = dataEmployee, BrandBudget = dataBrandBudget, PostBudget = dataPostBudget, Area = dataArea }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception ex)
            {
                return Json(new { Error = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

MVC 中的 LargeJsonResult 返回许多值

若要减少有效负载,请考虑进行 4 个单独的 ajax 调用,每个调用到返回所需 4 个属性之一的不同方法。

public ActionResult LoadInitEmployeeData()
{
  Employee dataEmployee =  ....
  ....
  return Json(dataEmployee, JsonRequestBehavior.AllowGet)
}
public ActionResult LoadBrandBudgetData()
{
  List<BrandBudget> dataBrandBudget = ....
  return Json(dataBrandBudget, JsonRequestBehavior.AllowGet)
}

等。