如何从代码后面设置此cookie属性的值?

时间:2010-10-08 13:23:49

标签: asp.net asp.net-mvc cookies properties

我想这样做:Dim str As String = class.isGuest("yes")但它不起作用。

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

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题,但我认为主要问题是您正在设置/创建名为“g”的Cookie,但您正在尝试检索名为MYACCOUNT_SESSION_COOKIE_NAME的Cookie。

您还可以通过调用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