Cookies not getting set in c#
I am using cookies to know whether a page was loaded before or not. So in page load of a asp.net c# page I am using this
if (Request.Cookies["PageLoaded"] == null)
{
//Initialize things if page loading for first time.
}
and inside the if as last parameter I am setting the cookies value like given below
if (Request.Cookies["PageLoaded"] == null)
{
//Initialize things if page loading for first time.
//Set cookies value to indicate page has loaded before
Response.Cookies["PageLoaded"].Value = "True";
}
When I run in local host its working fine. But when I host it to server for each page load(Postback events) the initial if statement is true(ie cookie is always null) and going inside the loop.
Am I doing something wrong? How can I do this in c开发者_如何学Go#? Thanks
Try setting an expiry date for your cookie, by default if you do not set an expiry date for the cookie it will be non-persistant and only stored as part of the Session information so when you close the browser the Cookie will be discarded e.g.
Response.Cookies["PageLoaded"].Value = "True";
Response.Cookies["PageLoaded"].Expires = DateTime.Now.AddDays(1);
精彩评论