Asp.net 小技巧 1 :解决__VIEWSTATE bug
https://blog.csdn.net/weixin_30823833/article/details/98411203
如果你在asp.net 的站点的网址后面加上这么一串?__VIEWSTATE=YY
例如:
你将得到一个类似这样的报错页面:
要解决这个问题其实也很简单:
在后台页面加上这么一段代码 就OK了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 protected override void OnInitComplete(EventArgs e)
2 {
3 base .OnInitComplete(e);
4 if (Request.QueryString.AllKeys.Contains( " __VIEWSTATE " ))
5 {
6 PropertyInfo info = typeof (System.Collections.Specialized.NameValueCollection).GetProperty( " IsReadOnly " , BindingFlags.Instance | BindingFlags.NonPublic);
7 if (info != null )
8 {
9 info.SetValue(Request.QueryString, false , null );
10 Request.QueryString.Remove( " __VIEWSTATE " );
11 info.SetValue(Request.QueryString, true , null );
12 }
13 }
14
2 {
3 base .OnInitComplete(e);
4 if (Request.QueryString.AllKeys.Contains( " __VIEWSTATE " ))
5 {
6 PropertyInfo info = typeof (System.Collections.Specialized.NameValueCollection).GetProperty( " IsReadOnly " , BindingFlags.Instance | BindingFlags.NonPublic);
7 if (info != null )
8 {
9 info.SetValue(Request.QueryString, false , null );
10 Request.QueryString.Remove( " __VIEWSTATE " );
11 info.SetValue(Request.QueryString, true , null );
12 }
13 }
14