2010-10-08 50 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