C# 中未设置 Cookie
我正在使用 cookie 来了解页面之前是否已加载。因此,在 asp.net c# 页面的页面加载中,我使用它
if (Request.Cookies["PageLoaded"] == null)
{
//Initialize things if page loading for first time.
}
,并在 if 作为最后一个参数中,我设置 cookies 值,如下所示
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";
}
当我在本地主机中运行时,它工作正常。但是,当我将其托管到服务器以进行每个页面加载(回发事件)时,初始 if 语句为 true(即 cookie 始终为空)并进入循环。
我做错了什么吗? 我怎样才能在 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#?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试为您的 cookie 设置到期日期,默认情况下,如果您不为 cookie 设置到期日期,它将是非持久性的,并且仅作为会话信息的一部分存储,因此当您关闭浏览器时,Cookie 将被丢弃,例如
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.