【发布时间】:2017-05-17 15:05:34
【问题描述】:
我正在尝试设置一个 cookie,如果存在则更新它。在初始设置中,cookie 看起来不错并且有过期日期,但是如果我刷新该页面,cookie 就会变成会话。
有什么想法吗?
Function writeReadArticleToCookie(ByVal id As Integer) As String()
Dim sarr As String()
Dim cookie As HttpCookie
cookie = Request.Cookies("read_articles")
If cookie Is Nothing
cookie = New HttpCookie("read_articles")
cookie.Path = "/"
cookie.Value = id.ToString() & ","
cookie.Expires = DateTime.Now.AddHours(6)
sarr = cookie.Value.Split(",")
Else
sarr = cookie.Value.Split(",")
If not sarr.Contains(id.ToString()) Then
cookie.Value = cookie.Value & id.ToString() & ","
cookie.Path = "/"
sarr = cookie.Value.Split(",")
End If
End If
Response.Cookies.Add(cookie)
Return sarr
End Function
【问题讨论】:
-
你没有在else中设置Expire,所以cookie使用默认的Expire,即Session。
-
@mattfei 实际上,问题是我正在更新请求而不是响应 cookie。