代码改变世界

在服务器端保存ViewState

2008-11-20 16:51  敏捷的水  阅读(3261)  评论(13编辑  收藏  举报

1.比较

Asp.net 的一个强大的功能就是ViewState,  但是这也成为很多人诟病的地方,我们先来看一个实例

ViewState其中一个特性就是保存页面的状态,下面我们看一个很简单的登录页面。

(1) 默认的页面存储

下面两幅图是使用默认的ViewState, Asp.net把状态保存在页面的一个Hidden控件里。每次请求时往返于客户端与服务器之间,我们可以看到储存了很多的值,页面大小是28.2K。

image

image

(2)自定义的Cache存储

下面两幅图是我们使用自定义的Cache存储,页面的大小是27.5K,虽然不到1K,但这个页面的结构比较简单,如果复杂的,如用了GridView等,这个差别还是比较大的

image

image

(3)扩展

实际上ASP.NET2.0 给我们提供了改变存储的功能,这个是通过覆盖Page的PageStatePersister

 protected override PageStatePersister PageStatePersister

 

而且ASP.NET2.0给内置了一个SessionPageStatePersister. 可以容易的把ViewState存到Session里

  return new SessionPageStatePersister(page);

 

2. 示例

这里我给出自定义的一个实例

(1)开发一个ViewStateFactory,根据不同的配置,修改存储方式。

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.Caching;

namespace TestViewState

{

    public enum ViewstateMode

    {

        Cache,

        Session,

        Page,

        File,

        Database

    }

    public class ViewStateFactory

    {

        public ViewStateFactory()

        {

            //

            // TODO: Add constructor logic here

            //

        }

        public static PageStatePersister CreateViewState(ViewstateMode mode, Page page)

        {

            switch (mode)

            {

                case ViewstateMode.Cache:

                    return new CacheStatePersister(page);

 

                case ViewstateMode.Session:

                    return new SessionPageStatePersister(page);

 

                case ViewstateMode.Page:

                case ViewstateMode.File:

                case ViewstateMode.Database:

                    return new HiddenFieldPageStatePersister(page);

                default:

                    return new HiddenFieldPageStatePersister(page); 

            }

        }

    }

//实现一个自定义的方式,存储到服务器的Cache里

    public class CacheStatePersister : PageStatePersister

    {

        private Cache mCache = HttpRuntime.Cache;

        public CacheStatePersister(Page page) : base(page) { }

 

        public override void Load()

        {

            string _vskey = Page.Request.Form["_VIEWSTATE_KEY"];

            if (_vskey == null)

            {

                base.ViewState = null;

            }

            else

            {

                base.ViewState = mCache[_vskey];

            }

        }

 

        public override void Save()

        {

            string _vskey;

            _vskey = "VIEWSTATE_" + HttpContext.Current.Session.SessionID + "_" + Page.Request.RawUrl +

               "_" + System.DateTime.Now.Ticks.ToString();

 

            mCache.Add(_vskey, base.ViewState, null,

                System.DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout), Cache.NoSlidingExpiration,

                            CacheItemPriority.Default, null);

 

            this.Page.RegisterHiddenField("_VIEWSTATE_KEY", _vskey);

        }

    }

}

(2)为了可以配置,我们在Web.config的AppSetting里加上

  <!--this is for where save viewstate, correct value is Cache, Page, Session-->

  <add key="ViewStateMode" value="Cache"/>

(3)覆盖页面的PageStatePersister,这里你可以开发一个BasePage,在基类里覆盖,所有的页面集成这个页面

protected override PageStatePersister PageStatePersister

        {

            get

            {

                ViewstateMode viewstateMode = ViewstateMode.Page;

                try

                {

                    switch (ConfigManager.ViewStateMode.ToLower())

                                    {

                                        case "cache":

                                            viewstateMode = ViewstateMode.Cache;

                                            break;

                                        case "session":

                                            viewstateMode = ViewstateMode.Session;

                                            break;

                                        default:

                                            viewstateMode = ViewstateMode.Page;

                                            break;

                                    }                                   

                                    return ViewStateFactory.CreateViewState(viewstateMode, this.Page);

                }

                catch (Exception ex)

                {

                    return base.PageStatePersister;

                }

 

            }

        }