Cookies and ASP.NET are driving me crazy

I created a simple cart. We need something specific for our needs, a long story. Anyway, I store the basket object in a cookie. This works well, but I am having trouble removing the cookie basket from the class. The cart object contains a collection of products (iList). Here is the code I use to delete the cookie: My empty recycle code:

Dim currentCookie As HttpCookie = HttpContext.Current.Response.Cookies(cookieName)
currentCookie.Expires = DateTime.Now.AddYears(-30)
HttpContext.Current.Response.Cookies.Add(currentCookie)

My code is LoadCartFromCookie:

if not HttpContext.Current.Request.Cookies(theCookieName) is nothing then
     _cart =  CType(HttpContext.Current.Request.Cookies(theCookieName).value,Cart)
End If

The basket class constructor first tries to load the basket from the cookie. If it finds a cookie, it loads the cart object, otherwise it creates a new cart instance without any details. For some reason, even if I run the delete cookie (Empty cart) code and then run the LoadCartFromCookie code (from inside the Cart class), it still loads the expired cookie. Any suggestions? I thought this might be a browser issue, but I tried IE8, FF 3.5, and Chrome. If I try to find an expired cookie (Request.Cookies (theName)) inside the codebehind and ASPX pages, it never finds it. This is what I want him to do inside the class.

Daniel

+3
source share
3 answers

?

cookie, cookie, cookie, no?

, 22, ....

cookie , . cookie , cookie.

cookie clr, , . cookie page_load cookie , Cookies.

p.s. , , Cookies.Remove() , , cookie . - cookie ...

+2

- , delete cookie (Empty cart), LoadCartFromCookie ( Cart), cookie.

, . Cookie cookie . "LoadCartFromCookie", .
. , .

+2

To expire a cookie attempt:

Dim cookie = HttpContext.Current.Request.Cookies(cookieName)
If Not cookie Is Nothing Then
    cookie.Expires = DateTime.Now.AddYears(-1)
    HttpContext.Current.Response.SetCookie(currentCookie)
End If
0
source

Source: https://habr.com/ru/post/1732694/


All Articles