每次刷新页面,判断HttpContext.Current.Response.Cookies["CityCookie"].Value都是null,代码如下:if (HttpContext.Current.Response.Cookies["CityCookie"].Value == null)
{
HttpContext.Current.Response.Cookies["CityCookie"].Value = "全国";
HttpContext.Current.Response.Cookies["CityCookie"].Expires = DateTime.Today.AddDays(365); }
{
HttpContext.Current.Response.Cookies["CityCookie"].Value = "全国";
HttpContext.Current.Response.Cookies["CityCookie"].Expires = DateTime.Today.AddDays(365); }
httpcookie.expires=datetime.now.adddats(1000000)
httpcookie.domain="xx.com";
Response.cookies.add(httpcookie)
如果cookies中一开始有值,这句话就不成立,赋值肯定不成功;你可以检查一下看看有没有值再看怎么解决。
cookies只要一离开本页就过期的问题,你可以建立永久cookies或者设定cookies过期时间,或者你的浏览器中的设定cookies过期时间的问题,你可以查查资料解决
HttpContext.Current.Response.Cookies["CityCookie"] == null
{
HttpCookie cookie = new HttpCookie("CityCookie");//添加cookie
cookie.Values.Add("CityCookie","全国");//增加属性
cookie.Expires = DateTime.Now.AddDays(365);
}