asp.net c#
我需要完成的功能如下
1、当用户进入网站浏览资源时,记录下用户浏览过的资源
2、当用户下次再到网站浏览资源时,显示出用户上次访问浏览过的资源记录
高手请解,如何实现上述功能,另外大家觉得用什么介质存储用户浏览记录好,是cookie,还是数据库;当然,本人希望有人能告诉我有没有第三种更好的选择

解决方案 »

  1.   

    在BasePage中记录用户访问记录
    页面继承BasePage
    用户级别要Server持久化
      

  2.   

    用cookie的话用户换一台电脑,所有记录都没有了。
    比较好的方法还是数据库吧,当然你要保存成文件也可以,跟保存到数据库性质上是一样的,只是这样不易维护。
      

  3.   

    当然是数据库好,用户有可能禁用Cookie或者清理掉Cookies
    提供一种思路,定义一个ViewTimes结构,保存各个页面(资源)的浏览情况,将此结构序列化为字节存进数组,为了提升性能,可以在Application_EndRequest事件中一次性写入数据库,并在Application_BeginRequest事件中初始化上面的结构(从数据库读取),甚至可以定义在MembershipProvider验证的时候一并读取数据,并自定义一个实现接口IPrincipal的类存放ViewTimes结构,可以跟Name等属性一并读取,是不错的思路[Serializable]
        struct ViewTimes
        {
            public int DefaultAspx;
            public int GameAspx;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.User.Identity.IsAuthenticated)//是否通过验证
            {
                //获取用户名
                string name = this.User.Identity.Name;
                ViewTimes vt = new ViewTimes();
                vt.DefaultAspx = 1;//给结构赋值,表示读取了资源
                vt.GameAspx = 2;
                BinaryFormatter bs = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bs.Serialize(ms, vt);//序列化为字节流
                byte[] buffer = ms.ToArray();转换为字节数组,此数组对应数据库Image格式字段
                //insert into data(viewtimes) values(buffer)更新数据库
            }
        }
    如果实现自己的MembershipProvider和Identity接口的类,就可以如下调用:
    Mydentity md = this.User.Identity as IMydentity
    md.viewtimes.DefaultAspx++;
    md.viewtimes.GameAspx++;
    无缝接入,呵呵,利用HttpApplication的Beginrequest和EndRequest事件,在用户一个访问流程中,只有一次读取数据库和写入数据库的操作,而且第一次是在验证用户的时候一并读取的,所以只增加了一次数据库操作
    自认为不错的选择,有时候我也实现一下看看
      

  4.   

    自定义模块加入,用此模块截获所有用户请求信息分析其URL,有效就++,无效就不处理,这样不用在每个页面加函数了,不用的时候在配置文件中取消模块注册就实行功能的卸载了
      

  5.   

    我的网站是这样做的:(以ascx文件为例)
    namespace Project
    {
      public class SmartControlUI:UserControl
      {
        private LiveMemberController _authorizator;
        public LiveMemberController Authorizator
        {
           get
           {
              return (this.Page as PageUI)==null?_authorizator=(this.Page as PageUI).Authorizator:null;
           }
           set
           {
              _authorizator=value;
           }
        }
      }
    }public class BrowseRecordPage:SmartControlUI
    {
       public System.Int32 memberid=0;
       protected override void Load(EventArg e)
       {
          memberid=this.Authorizator.LoginInfo.ID;
          browseringHistoryList = BLL.Logic.GoodsBrowseringHistory.GetList(topCount, memberId, this.SiteID, new IStruct<IGoodsBrowseringHistoryStruct>[] { MyStruct.Mall.GoodsBrowseringHistory.GoodsVersionID });
       }
    }同时在具体的产品版本展示页处理如下:public class ProductPage:PageUI
    {
       private System.Int32 memberid;
       protected overide void OnLoad(EventArgs e)
       {
          base.OnLoad(e);
          memberid=this.Authorizator.LoginInfo.ID;
          AddGoodsBrowseringHistories();
       }
    public void AddGoodsBrowseringHistories()
    {
    if (!BLL.Logic.GoodsBrowseringHistory.IsExtGoodsBrowseringHistory(goodsVersionId, this.Authorizator.LoginInfo.ID))
    {
    GoodsBrowseringHistoryInfo historyInfo = new GoodsBrowseringHistoryInfo();
    historyInfo.GoodsVersionID = goodsVersionId;
    historyInfo.SiteID = this.DestSiteID;
    historyInfo.MemberID = Authorizator.LoginInfo.ID;
    historyInfo.AddTime = DateTime.Now;
    BLL.Logic.GoodsBrowseringHistory.Add(historyInfo);
    }           
                if (dataStatisticInfo == null)
                {
                    dataStatisticInfo = new GoodsDataStatisticInfo();
                    dataStatisticInfo.GoodsID = goodsId;
                    dataStatisticInfo.GoodsVersionID = goodsVersionId;
                    dataStatisticInfo.View =1;
                    BLL.Logic.GoodsDataStatistic.Add(dataStatisticInfo);
                }
                else
                {
                    Dictionary<IStruct<IGoodsDataStatisticStruct>, object> dicDataStatisticFields = new Dictionary<IStruct<IGoodsDataStatisticStruct>, object>();
                    dicDataStatisticFields.Add(MyStruct.Mall.GoodsDataStatistic.View, dataStatisticInfo.View + 1);
                    BLL.Logic.GoodsDataStatistic.Modify(dataStatisticInfo.ID, dicDataStatisticFields);
                }
    }
    }