本文介绍了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中失去价值
发布评论