服务器端ViewState的实现

在B/S项目开发中经常会遇到由于ViewState的数据量太大而产生Client与Server进行交互时的网络传输数据量过大从而导致效率低下的现象。为了解决这个问题可以采用服务器端ViewState的方法,将不影响页面显示的对象保存到服务器端的硬盘上,这种方法虽然没有使用Session的速度快,但是可以减少Client与Server的数据交互因此速度要远比发送到客户端高很多,与此同时由于数据保存到硬盘上还可以解决大数据量的问题。
可以使项目中的每个页面从一个基类去继承,在这个基类中实现如下的方法和属性。
ServerViewState的定义:
None.gif          private  StateBag _serverViewState = null  ;
None.gif        
protected  StateBag ServerViewState
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(_serverViewState==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _serverViewState
=new StateBag(this.ViewStateIgnoresCase) ;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _serverViewState;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

定义ServerViewState序列化后数据的保存路径:
None.gif
None.gif        
private   string  ServerViewStatePath
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
string _strPath = Server.MapPath(Context.Request.ApplicationPath) + "/ServerViewState/"+Session.SessionID+"/";
InBlock.gif                
if(!Directory.Exists(_strPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Directory.CreateDirectory(_strPath);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
string _fileName=this.GetType().BaseType.FullName+".ViewState";
InBlock.gif                _strPath 
+= _fileName;
InBlock.gif                _strPath 
= _strPath.Replace('\\','/');
InBlock.gif                
return _strPath;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

保存ServerViewState的方法:
None.gif protected   override   object  SaveViewState()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(_serverViewState!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object serverViewState=SaveServerViewState();
InBlock.gif                SavePageServerStateToPersistenceMedium(serverViewState);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return base.SaveViewState ();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   object  GetServerViewStateObject(StateBag serverViewState)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            ArrayList keyList 
= null;
InBlock.gif            ArrayList valueList 
= null;
InBlock.gif            
if (serverViewState.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IDictionaryEnumerator enumerator1 
= serverViewState.GetEnumerator();
InBlock.gif                
while (enumerator1.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    StateItem item1 
= (StateItem) enumerator1.Value;
InBlock.gif                    
if (keyList == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        keyList 
= new ArrayList(5);
InBlock.gif                        valueList 
= new ArrayList(5);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    keyList.Add(enumerator1.Key);
InBlock.gif                    valueList.Add(item1.Value);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keyList != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return new Pair(keyList, valueList);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
protected   virtual   object  SaveServerViewState()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (this._serverViewState != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return GetServerViewStateObject(this._serverViewState);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedBlockEnd.gif        }

None.gif
private   void  SavePageServerStateToPersistenceMedium( object  serverViewState)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            StringBuilder _sb
=new StringBuilder() ;
InBlock.gif            TextWriter _writer
=new StringWriter(_sb); 
InBlock.gif            _formatter.Serialize(_writer,serverViewState) ;
InBlock.gif            
string _strPath=ServerViewStatePath;
InBlock.gif            
if(File.Exists(_strPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                File.Delete(_strPath);
ExpandedSubBlockEnd.gif            }

InBlock.gif            FileStream fs
=new FileStream(_strPath,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None) ;
InBlock.gif            
using(StreamWriter sw=new StreamWriter(fs))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sw.Write(_sb.ToString());
InBlock.gif                sw.Close() ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            fs.Close() ;
ExpandedBlockEnd.gif        }

读取ServerViewState的方法:
None.gif protected   override   void  LoadViewState( object  savedState)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
object viewState=LoadPageServerStateFromPersistenceMedium();
InBlock.gif            
if(viewState!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._serverViewState=GetServerViewStateFromObject(viewState);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this._serverViewState=null;
InBlock.gif            
base.LoadViewState (savedState);
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   object  LoadPageServerStateFromPersistenceMedium()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string _strPath=ServerViewStatePath;
InBlock.gif            
if(!File.Exists(_strPath) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            FileStream fs
=new FileStream(_strPath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite) ;
InBlock.gif            
string _viewState=null;
InBlock.gif            
using(StreamReader sr=new StreamReader(fs) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _viewState
=sr.ReadToEnd() ;
InBlock.gif                sr.Close() ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            fs.Close() ;
InBlock.gif            File.Delete(_strPath) ;
InBlock.gif            
if(_viewState==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return _formatter.Deserialize(_viewState) ;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private  StateBag GetServerViewStateFromObject( object  viewState)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Pair _pair
=viewState as Pair;
InBlock.gif            
if(_pair!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StateBag state
=new StateBag(this.ViewStateIgnoresCase) ;
InBlock.gif                ArrayList keyList
=_pair.First  as ArrayList;
InBlock.gif                ArrayList valueList
=_pair.Second as ArrayList;
InBlock.gif                
for (int i = 0; i < keyList.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    state.Add((
string) keyList[i], valueList[i]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return state;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedBlockEnd.gif        }

这样就可以在每个子页面中就可以直接调用ServerViewState,使用方法与ViewState完全相同,而不用顾虑后台的实现方式。

转载于:https://www.cnblogs.com/firstsee/archive/2005/07/19/195739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值