2010-10-08 61 views
1

我想這樣做:Dim str As String = class.isGuest("yes")但它不會工作。如何從後面的代碼中設置此Cookie屬性的值?

Public Property IsGuest(ByVal guestStatus As String) As String 
    Get 
     Dim guestCookie As New HttpCookie("g") 

     For Each key As String In Context.Response.Cookies.Keys 
      If key = MYACCOUNT_SESSION_COOKIE_NAME Then 
       guestCookie = Context.Response.Cookies.Item(MYACCOUNT_SESSION_COOKIE_NAME) 
       Exit For 
      End If 
     Next 

     guestCookie.Value = guestStatus 
     Response.Cookies.Add(guestCookie) 

     Return guestCookie.Value.ToString 
    End Get 
    Set(ByVal value As String) 
     Dim guestCookie As New HttpCookie("g") 

     guestCookie.Value = value 
     Response.Cookies.Add(guestCookie) 
    End Set 
End Property 

回答

0

有代碼中的一些問題,但我認爲主要的問題是,你要設置/創建一個名爲「G」的cookie,但你正試圖獲取一個名爲MYACCOUNT_SESSION_COOKIE_NAME餅乾。

您還可以通過調用Cookie集合上的屬性來替代您的循環來簡化您的代碼,該集合執行相同的操作。

Public Property IsGuest(ByVal guestStatus As String) As String 
    Get 
     Return Context.Response.Cookies(MYACCOUNT_SESSION_COOKIE_NAME).Value 
    End Get 
    Set(ByVal value As String) 
     Response.Cookies.Add(New HttpCookie(MYACCOUNT_SESSION_COOKIE_NAME, value) 
    End Set 
End Property 
相關問題