vb.net ASP.NET/VB 读取/写入 Cookies - 对象未设置为对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27899190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:39:23  来源:igfitidea点击:

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

asp.netvb.netcookies

提问by Billy Brown

so I have this code:

所以我有这个代码:

        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

I'm trying to read/write to a cookie, but everytime I run this, I get a "Object not set to an instance of an object" error.

我正在尝试读取/写入 cookie,但每次运行它时,都会收到“对象未设置为对象的实例”错误。

Does anyone know why?

有谁知道为什么?



I changed the code slighty, still the same error? Per the comment below, I do check to see if the value is nothing.

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

            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


ANOTHER EDIT:

另一个编辑:

I added a breakpoint at the Try line, guess, what it doesn't hit. "No symbol information loaded etc". I tried loading the DLL manually, I've rebuilt the solution etc, no difference?

我在 Try 行添加了一个断点,猜猜它没有命中什么。“未加载符号信息等”。我尝试手动加载 DLL,我重建了解决方案等,没有区别吗?

回答by Mairaj Ahmad

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

您收到此异常是因为您正在尝试读取 cookie 集合中不存在的 cookie 值。

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

Try this for C#

试试这个 C#

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

In VB.Net

在 VB.Net 中

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