我有一个 aspx 页面,它计算每次访问并创建一个 cookie。但是如果我使用 OutputCache 页面只计算请求该页面的第一个访问者。我怎样才能防止这个错误?
页面指令:
<%@ OutputCache Duration="1200" VaryByParam="mode;page;sid;tid" %>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
//Load single post data
#region Hit Counter
//hit counter lasts during session
if (Session["LastHit" + postId] == null)
{
cmmnd.CommandText = "UPDATE Posts SET Hits=Hits+1 WHERE PostID=@PostID;";
cmmnd.ExecuteNonQuery();
Session["LastHit" + postId] = 1;
}
#endregion
}
请您参考如下方法:
这不是错误,而是设计使然。如果页面在缓存中并且“缓存的输出仍然有效”,则不会重新处理该页面。您可以在此验证阶段运行代码,这可以帮助您执行计算访问次数并将其添加到 cookie 的任务。以下内容可能会有所帮助:
