ASP.NET/VB Reading/Writing Cookies - 对象未设置为对象的实例

ASP.NET/VB Reading/Writing Cookies - Object not set to instance of an object

所以我有这个代码:

        Try
        If Request.Cookies("curUsrId")("id") Is Nothing Then
            Dim cke As HttpCookie = New HttpCookie("curUsrId")
            cke("id") = CStr(myUser.Id)
            cke.Expires = Now.AddDays(35)
            Response.Cookies.Add(cke)
        Else
            If Request.Cookies("curUsrId")("id") = "2" Then
                grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowAllRecords
                chkPaging.Checked = True
            Else
                grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowPager
                chkPaging.Checked = False
            End If
        End If
    Catch ex As Exception
        lblErrorMsg.Visible = True
        txtErrorTxt.Visible = True
        txtErrorTxt.Text = ex.Message
    End Try

我正在尝试 read/write 到 cookie,但每次我 运行 这样做时,我都会收到 "Object not set to an instance of an object" 错误。

有人知道为什么吗?


我稍微改了一下代码,还是一样的错误?根据下面的评论,我会检查该值是否为空。

            Try
            If Request.Cookies("curUsrId").Value Is Nothing Then
                Dim cke As HttpCookie = New HttpCookie("curUsrId")
                cke.Value = CStr(myUser.Id)
                cke.Expires = Now.AddDays(35)
                Response.Cookies.Add(cke)
            Else
                If Request.Cookies("curUsrId").Value = "2" Then
                    grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowAllRecords
                    chkPaging.Checked = True
                Else
                    grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowPager
                    chkPaging.Checked = False
                End If
            End If
        Catch ex As Exception
            lblErrorMsg.Visible = True
            txtErrorTxt.Visible = True
            txtErrorTxt.Text = ex.Message
        End Try

另一个编辑:

我在 Try 行添加了一个断点,猜猜它没有命中什么。 "No symbol information loaded etc"。我尝试手动加载 DLL,重新构建解决方案等,没有区别吗?

You are getting this exception because you are trying to read value of a cookie which does not exist in cookies collection.

If Request.Cookies("curUsrId").Value //Here you are trying to read value from cookie wich is not set yet

为 C# 尝试这个

if(Request.Cookies.Get("curUsrId")==null)
{
//Your code to add cookie
}

在VB.Net

If Request.Cookies.Get("curUserId") Is Nothing Then