You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提交大数据到DataSourceRequest时Kendo.Mvc.dll抛出System.StackOverflowException

Solution for KendoGrid StackOverflowException with Large Filter in ASP.NET MVC

I've run into this exact issue before when dealing with overly complex KendoGrid filters, and the root cause is almost always the recursive parsing logic in Kendo.Mvc's default DataSourceRequestModelBinder hitting the CLR stack limit when processing huge filter expressions. Here are the most effective fixes I've used:

1. Optimize Your Filter Expression (Best First Step)

First, take a look at the filter your custom logic is generating—chances are there's a lot of redundancy you can cut down:

  • Replace multiple OR conditions on the same field with the in operator. For example, instead of generating separate eq filters for each ID, use { field: "Id", operator: "in", value: [1, 2, 3, ...] }—this drastically reduces the filter string length.
  • Use range operators (gte, lte) for date/number ranges instead of multiple individual conditions.
  • Remove any redundant or duplicate filter rules that might be getting added accidentally.

2. Implement a Custom DataSourceRequest Model Binder

The default Kendo binder uses recursive parsing for filter descriptors, which blows the stack with large filters. Replace it with a custom binder that uses non-recursive JSON deserialization:

Step 1: Create the Custom Binder

using Kendo.Mvc.UI;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.Mvc;

public class CustomDataSourceRequestModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var request = new DataSourceRequest();
        var httpRequest = controllerContext.HttpContext.Request;

        // Parse pagination parameters
        request.Page = int.TryParse(httpRequest["page"], out int page) ? page : 1;
        request.PageSize = int.TryParse(httpRequest["pageSize"], out int pageSize) ? pageSize : 10;

        // Parse sorting parameters
        var sortJson = httpRequest["sort"];
        if (!string.IsNullOrEmpty(sortJson))
        {
            request.Sorts = JsonConvert.DeserializeObject<List<SortDescriptor>>(sortJson);
        }

        // Parse filter with non-recursive deserialization
        var filterJson = httpRequest["filter"];
        if (!string.IsNullOrEmpty(filterJson))
        {
            // Use Newtonsoft.Json to directly deserialize the filter (avoids Kendo's recursive parsing)
            request.Filter = JsonConvert.DeserializeObject<CompositeFilterDescriptor>(filterJson);
        }

        return request;
    }
}

Step 2: Register the Binder

Add this to your Global.asax.cs (or Startup.cs if using ASP.NET MVC 5+ with OWIN):

protected void Application_Start()
{
    // ... other startup code
    ModelBinders.Binders.Add(typeof(DataSourceRequest), new CustomDataSourceRequestModelBinder());
}

This bypasses Kendo's problematic recursive parser and uses Newtonsoft.Json's efficient deserialization which doesn't hit stack limits even with large filters.

3. Adjust Request Size Limits (If Using POST)

If you're sending the filter via POST (which you should be for large payloads), make sure your web.config allows large enough requests:

<system.web>
    <!-- Max request size in KB (20MB here) -->
    <httpRuntime maxRequestLength="20480" />
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <!-- Max content length in bytes (20MB here) -->
            <requestLimits maxAllowedContentLength="20971520" />
        </requestFiltering>
    </security>
</system.webServer>

Also ensure your Kendo DataSource is configured to use POST:

$("#grid").kendoGrid({
    dataSource: {
        type: "POST",
        url: "/Controller/CustomAction",
        // ... other config
    }
});

As a last resort, you can increase the CLR stack size for your application, but this is a band-aid and can lead to other memory issues. To do this:

  1. Right-click your project → Properties → Build → Advanced.
  2. Set "Base address and stack size" to a larger value (e.g., 256MB).

I strongly advise against this long-term, but it can get you up and running while you implement the proper fixes above.


内容的提问来源于stack exchange,提问作者Nirman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:07:47