如何设置Cookie值? [英] How to set cookie value?

查看:164
本文介绍了如何设置Cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下操作来设置cookie值:

I'm doing the following to set a cookie value:

HttpCookie mycookie = new HttpCookie("mycookie");
mycookie.value = "value1";
mycookie.Expires = DateTime.Now.Add(1);
HttpContext.Current.Response.Cookies.Add(mycookie);

有时,我使用以下方法检查Cookie:

Sometime later, I check the cookie using:

HttpCookie mycookie = HttpContext.Current.Request.Cookies["mycookie"];

我注意到它仍然具有较旧的值:

I notice it still has an older value:

mycookie.value == "oldValue"

设置Cookie后,我什至可以立即检查Cookie,而设置的值不存在.仍然是旧值.

I can even check the cookie immediately after setting it and the value I've set isn't there. It's still the old value.

未设置该值怎么办?我该如何设置?

What is happening that the value isn't being set and how can I set it?

推荐答案

尝试一下,您需要先删除然后添加

Try this, you need to remove it and then add it

var response = HttpContext.Current.Response;
response.Cookies.Remove("mycookie");
response.Cookies.Add(cookie);

这篇关于如何设置Cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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