隐藏源文件的VIEWSTATE

 默认情况下,为所有服务器控件启用视图状态。若要禁用视图状态,请将控件的  EnableViewState  属性设置为  false,如下面的  DataGrid  服务器控件示例所示。   
  <asp:datagrid  EnableViewState="false"  datasource="..."  runat="server"/>

....................正菜开始了......................................................................

为什么会出这个错的啊


编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。

编译器错误消息: CS0103: 当前上下文中不存在名称“DomainBase”

源错误:



行 21:            if (_Dir == null)
行 22:            {
行 23:                _Dir = new DirectoryInfo(Path.Combine(DomainBase.Extention.BaseDirPath, "App_Data"));
行 24:                if (!_Dir.Exists)
行 25:                    _Dir.Create();



我的前台页面是 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeater.aspx.cs" Inherits="repeater" %>


后台是:public partial class repeater : XVpage


然后在app_Code做了一个叫XVpage.cs的类

C# code
using System; using System.IO; using System.Web.UI; public class XVPage : Page { static private DirectoryInfo _Dir; private DirectoryInfo Dir { get { if (_Dir == null) { _Dir = new DirectoryInfo(Path.Combine(DomainBase.Extention.BaseDirPath, "App_Data")); if (!_Dir.Exists) _Dir.Create(); _Dir = new DirectoryInfo(Path.Combine(_Dir.FullName, "ViewState")); if (!_Dir.Exists) _Dir.Create(); } return _Dir; } } protected override object LoadPageStateFromPersistenceMedium() { PageStatePersister ps = this.PageStatePersister; ps.Load(); if (ps.ControlState != null) ps.ControlState = 反序列化对象((string)ps.ControlState); if (ps.ViewState != null) ps.ViewState = 反序列化对象((string)ps.ViewState); return new Pair(ps.ControlState, ps.ViewState); } protected override void SavePageStateToPersistenceMedium(object state) { PageStatePersister ps = this.PageStatePersister; if (state is Pair) { Pair pair = (Pair)state; ps.ControlState = pair.First; ps.ViewState = pair.Second; } else { ps.ViewState = state; } if (ps.ControlState != null) ps.ControlState = 序列化对象(ps.ControlState); if (ps.ViewState != null) ps.ViewState = 序列化对象(ps.ViewState); ps.Save(); } private object 反序列化对象(string stateID) { string stateStr = (string)Cache[stateID]; string file = Path.Combine(Dir.FullName, stateID); if (stateStr == null) stateStr = File.ReadAllText(file); else Cache.Remove(stateID); return new ObjectStateFormatter().Deserialize(stateStr); } private string 序列化对象(object obj) { string value = new ObjectStateFormatter().Serialize(obj); string stateID = (DateTime.Now.Ticks + (long)value.GetHashCode()).ToString(); //产生离散的id号码 File.WriteAllText(Path.Combine(Dir.FullName, stateID), value); Cache.Insert(stateID, value); return stateID; } protected override void OnUnload(EventArgs e) { base.OnUnload(e); DateTime dt = DateTime.Now.AddMinutes(-20); foreach (FileInfo fl in Dir.GetFiles()) if (fl.LastAccessTime < dt) try { fl.Delete(); } catch { } } } 哦,那是另一个class,我在那里去读取web.config等环境参数来看看用户有没有另外手动设置根目录。这里,你可以直接改为 AppDomain.CurrentDomain.BaseDirectory。
var _Dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data"));

这样,总是在你的网站的应用程序目录下去查找 App_Data 子目录,这也没有任何运行错误。

有时候,例如我的生产服务器上,将数据库放在网站以外,所以我在web.config中可以配置目录。所以使用一个工具方法来判断数据库目录。
////////////////////////////////////
///////////////////////////////////////
无法将类型为“System.Collections.Specialized.HybridDictionary”的对象强制转换为类型“System.String”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 无法将类型为“System.Collections.Specialized.HybridDictionary”的对象强制转换为类型“System.String”。

源错误:


行 39:        ps.Load();
行 40:        if (ps.ControlState != null)
行 41:            ps.ControlState = 反序列化对象((string)ps.ControlState);
行 42:        if (ps.ViewState != null)
行 43:            ps.ViewState = 反序列化对象((string)ps.ViewState);


源文件: d:\chinawl\App_Code\XVPage.cs    行: 41

还是出错,,,
遇到 ps.ControlState 无法转换为 string 还真是诡异呢!你使用的某一个非正式由asp.net项目组发布和技术支持的客户端(javascript端)的扩展组件它大概很“狠”地在客户端去修改了隐藏域中的数据。如果还是使用它,你可以尝试将
C# code
if (ps.ControlState != null) ps.ControlState = 反序列化对象((string)ps.ControlState);

C# code
if (ps.ControlState != null) ps.ControlState = 序列化对象(ps.ControlState);
注释掉。毕竟,内容巨大是ViewState部分,我们可以对ControlState部分不做处理仍然丢到客户端去。如果注释掉这两行代码仍然出现同样问题,你最好先试试找出是哪一个第三方组件的问题。


   本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

原文地址:https://www.cnblogs.com/wzg0319/p/1606195.html