URL-encoded form data is not valid ,Operation is not valid due to the current state of the object

System.Web.HttpUnhandledException: 引发类型为“System.Web.HttpUnhandledException”的异常。 ---> System.Web.HttpException: URL 编码窗体数据无效。 ---> System.InvalidOperationException: 对象的当前状态使该操作无效。 在 System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded() 在 System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) 在 System.Web.HttpRequest.FillInFormCollection() --- 内部异常堆栈跟踪的结尾 --- (后面省略)

如果数量少的话,不会出现此问题,当数据库较大此,就会出现此问题。

提交的表单数据太多了,你可以

1,更新到最新的系统补丁和.net补丁

2,可以在web.config里面写

<appSettings> 
<add key="aspnet:MaxHttpCollectionKeys" value="5000" /> 
<add key="aspnet:MaxJsonDeserializerMembers" value="5000" /><!-- 不使用json,这行可不要 -->
</appSettings>

Operation is not valid due to the current state of the object

在一个 asp.net webform 的程序 postback 时,碰到了这个异常,而且无法调试。

Google 了一下,发现原因原来是 asp.net 对一个 form post 的字段数有限制,在打过最新的安全更新后,这个限制的数目是1000个,超过了就会报这个异常。

解决的办法是在 web.config 里面加一个设置,增大这个限制值:

 <appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="2001" />
 </appSettings>

而为什么会有这么多 field 被提交呢,我这个页面用了一个 Telerik 的 RadGrid,其中每一行都有一个 checkbox 进行多选提交,但记录数才100多条,远没到1000.

看来原因可能是 telerik Grid 生成的 html 里面,多生成了很多非必要的表单字段,用来维护一些状态信息之类的,导致表单提交的字段数膨胀了几乎10倍。如果改用轻量级的表格自己实现的话,优化的空间还是很大的

原文地址:http://www.jianfangkk.com/aspnet/201511/297

原文地址:https://www.cnblogs.com/jianfangkk/p/5107972.html