Cookie在ASP.net中失去价值

编程入门 行业动态 更新时间:2024-10-27 17:18:33
本文介绍了Cookie在ASP中失去价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码设置cookie:

I have the following code that sets a cookie:

string locale = ((DropDownList)this.LoginUser.FindControl("locale")).SelectedValue; HttpCookie cookie = new HttpCookie("localization",locale); cookie.Expires= DateTime.Now.AddYears(1); Response.Cookies.Set(cookie);

但是,当我尝试读取Cookie时,值为Null。 Cookie存在。我从不通过以下if检查:

However, when I try to read the cookie, the Value is Null. The cookie exists. I never get past the following if check:

if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))

帮助? 推荐答案

检查是在回发之后完成的吗?如果是这样的话,您应该改为从Request集合中读取Cookie。

The check is done after a post back? If so you should read the cookie from the Request collection instead.

通过将Cookie添加到Response.Cookies并将其保存到浏览器中,并从Request.Cookies中读取它们。

The cookies are persisted to the browser by adding them to Response.Cookies and are read back from Request.Cookies.

添加到Response的cookie仅在页面在同一请求上时才能读取。

The cookies added to Response can be read only if the page is on the same request.

更多推荐

Cookie在ASP.net中失去价值

本文发布于:2023-11-09 22:38:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:价值   Cookie   ASP   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!