I have an aspx page which counts every visit and creates a cookie. But if I use OutputCache page counts only the first visitor who requested the page. How can I prevent this bug?
Page directive:
<%@ OutputCache Duration="1200" VaryByParam="mode;page;sid;tid" %>
Codebehind:
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
}
This isn’t a bug, but by design. The page is not re-processed if it is in the cache and “the cached output is still valid”. You can run code during this validation phase and this could help you perform the task of counting the visits and adding it to a cookie. The following might be of help: