Cookie在ASP.net中失去价值 [英] Cookie loses value in ASP.net

查看:74
本文介绍了Cookie在ASP.net中失去价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码设置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中失去价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆