使用Cookie来统计浏览次数,当天重复刷新不增加

时间:2021-08-15 12:54:24

这是一种不严谨的做法,在浏览量不是很重要的时候可以使用

    var oldCookie = Request.Cookies["newsCookie"];
if (oldCookie == null)
{
var newsCookie = new HttpCookie("newsCookie");
newsCookie.Expires = DateTime.Now.Date.AddDays();
newsCookie.Values.Add("" + newsId + "", "'" + newsId + "'");
Response.SetCookie(newsCookie);
model.ClickCount = model.ClickCount + ;
_newsService.Update(model);
}
else
{
if (oldCookie["" + newsId + ""] == null)
{
oldCookie.Values.Add("" + newsId + "", "'" + newsId + "'");
Response.AppendCookie(oldCookie);
model.ClickCount = model.ClickCount + ;
_newsService.Update(model);
}
}