asp.net c#
我需要完成的功能如下
1、当用户进入网站浏览资源时,记录下用户浏览过的资源
2、当用户下次再到网站浏览资源时,显示出用户上次访问浏览过的资源记录
高手请解,如何实现上述功能,另外大家觉得用什么介质存储用户浏览记录好,是cookie,还是数据库;当然,本人希望有人能告诉我有没有第三种更好的选择
我需要完成的功能如下
1、当用户进入网站浏览资源时,记录下用户浏览过的资源
2、当用户下次再到网站浏览资源时,显示出用户上次访问浏览过的资源记录
高手请解,如何实现上述功能,另外大家觉得用什么介质存储用户浏览记录好,是cookie,还是数据库;当然,本人希望有人能告诉我有没有第三种更好的选择
解决方案 »
- WPF的Button,能否添加图标?
- 关于C#中TCP接收数据的问题!!请高手来回答下
- 初学者,关于C#中的string
- 如何解决“值不能未空,参数名:datdSet()“这个异常???
- 调用摄像头拍照
- 保存数据的一个问题,在线等!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 三青年之C#版 欢迎大家补充
- StreamReader读取的文件怎么是乱码?求高人指点
- 无法识别的配置节“AppSettings”----在线等
- 求 c#数据库经典入门电子书 (WROX 出版社)和 visual c# 开发实践 (人民邮电)
- asp.net 有什么好的方法可以改变Application占内存么
- 关于VS自带打包的问题
页面继承BasePage
用户级别要Server持久化
比较好的方法还是数据库吧,当然你要保存成文件也可以,跟保存到数据库性质上是一样的,只是这样不易维护。
提供一种思路,定义一个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事件,在用户一个访问流程中,只有一次读取数据库和写入数据库的操作,而且第一次是在验证用户的时候一并读取的,所以只增加了一次数据库操作
自认为不错的选择,有时候我也实现一下看看
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);
}
}
}