MVC中的LargeJsonResult返回许多值

时间:2014-11-28 02:15:15

标签: c# asp.net asp.net-mvc json asp.net-mvc-4

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

我已经使用它添加了我的web.config。但错误仍然存​​在。

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

然后我在这个网站http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/

上添加新类LargeJsonResult

它在控制器中这样说了

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);
            }
        }

1 个答案:

答案 0 :(得分:1)

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

public ActionResult LoadInitEmployeeData()
{
  Employee dataEmployee =  ....
  ....
  return Json(dataEmployee, JsonRequestBehavior.AllowGet)
}

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