weixin_33725272 2015-08-27 17:04 采纳率: 0%
浏览 181

从Web API获取大型JSON

I am using .net Web Api to get data for my data grid. The call is made via ajax call like this

   type: 'GET',
   dataType: 'json',
   contentType: "application/json; charset=utf-8",
   url: ReportURL, // "api/AppData/InvoiceReport/10"
   success: function (mydata) {
     createReportGrid(myData); // this function creates a KENDO grid
   error: function (error) {

The Web API method Looks like this

public HttpResponseMessage InvoiceReport(int Id)
  // some llogic of data retrieving
  // objReportDataList is of Type List<vmReport>
  // thisstructure contains a DataTable, and 2 more list type  
 return Request.CreateResponse(HttpStatusCode.OK, objReportDataList);

These calls work perfectly for Rows Count approx 100K rows

Web Api serializes perfectly. But when the Row Count exceeds 200K i get 500 internal server error The stack traces tells "System-OutOfMemoryException-occured-in-mscorlib-dll"

NOTE - I cant use server pagination to get little data only. This Million rows data is working on a ASP.NET Webforms application. We have migrated to MVC pattern and used WebApi for fetching Data but this error is occuring. PS - I have tried many many solutions, but helpless

Please Guide me to Get this error removed and my reports get going

  • 写回答

2条回答 默认 最新

  • weixin_33730836 2015-08-27 17:40

    you can add below in web.config file

                   <jsonSerialization maxJsonLength="50000000"/>
  • weixin_33682790 2015-10-29 22:02

    you should stream the response. then you can return millions of rows which are sent in chunks.

    public HttpResponseMessage PushStreamContent()
        var response = Request.CreateResponse();
        response.Content = 
            new PushStreamContent((stream, content, context) =>
            foreach (var staffMember in _staffMembers)
                var serializer = new JsonSerializer();
                using (var writer = new StreamWriter(stream))
                    serializer.Serialize(writer, staffMember);
        return response;

    more info here: http://dblv.github.io/2014/07/02/streaming-web-api/




  • ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
  • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料
  • ¥15 51单片机的外部中断,按下按键后不能切换到另一个模式
  • ¥15 java连接sqlserver有问题
  • ¥15 yolov8 如何调cfg参数
  • ¥15 这个四人抢答器代码哪儿有问题?仿真程序怎么写?
  • ¥15 burpsuite密码爆破
  • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
  • ¥15 scikit安装之后import不了
  • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊