ASP.NET 的 null cookie 问题

发布于 2024-10-04 00:31:26 字数 1534 浏览 5 评论 0原文

我在编写 If 语句时遇到问题,因为它要求 cookie 的值,但 cookie 可能为 null,这会破坏页面。

这是代码:

If Request.Cookies("myCookie").Value = "1234" then
'do stuff
End If

我想我需要一种优雅的方式来表达“如果 myCookie 不为空并且值为...”

有人有任何想法吗?

编辑:

好的,让它工作,但是由于有很多 if 和 else if 将会发生,我想知道是否有更好的方法来做到这一点...

If not Request.Cookies("myCookie") is nothing and Request.Cookies("myCookie").Value = "1234" then
    'do this
Else If not Request.Cookies("myCookie") is nothing and Request.Cookies("myCookie").Value = "5678" then
    'do that
Else
    'do something else
End If

好的...感谢所有回复...不确定这将是最好的方法,但我会尝试所有方法并接受最好的答案。

我还将给这个问题增加另一个层次的复杂性:

   If Not Request.Cookies("myCookie") is Nothing Then
        Select Case Request.Cookies("myCookie").Value
                Case "EZ12"
                theNumber = "0800 111 1111"
Case "ER34"
                theNumber = "0800 333 3333"
Case "RE32"
                theNumber = "0800 444 4444"
                Case Else
                    theNumber = "0800 222 2222"
        End Select
    Else
        Select Case Request.Cookies("myCookie2").Value
                Case "EZ12"
                theNumber = "0800 111 1111"
Case "ER34"
                theNumber = "0800 333 3333"
Case "RE32"
                theNumber = "0800 444 4444"
                Case Else
                    theNumber = "0800 222 2222"
        End Select
    End If

基本上情况总是相同的,但它会根据 myCookie 是否有值从两个位置之一选择情况。

由于可能有相当多的案例,无论如何我都可以只列出一次。?

I'm having problems writing an If statement because it's asking for the value of a cookie, but it's possible that the cookie could be null which breaks the page.

Here's the code:

If Request.Cookies("myCookie").Value = "1234" then
'do stuff
End If

I think i need an elegant way of say "If myCookie is not null and has a value of..."

Anyone got any thoughts?

EDIT:

OK, got it working but since there's a lot of if's and else if's going to be going on, I wonder whether there's a better way to do this...

If not Request.Cookies("myCookie") is nothing and Request.Cookies("myCookie").Value = "1234" then
    'do this
Else If not Request.Cookies("myCookie") is nothing and Request.Cookies("myCookie").Value = "5678" then
    'do that
Else
    'do something else
End If

OK... thanks for all the replies... not sure which is going to be the best approach but i'll try them all and accept the best as the answer.

I am also going to add another level of complexity to the question:

   If Not Request.Cookies("myCookie") is Nothing Then
        Select Case Request.Cookies("myCookie").Value
                Case "EZ12"
                theNumber = "0800 111 1111"
Case "ER34"
                theNumber = "0800 333 3333"
Case "RE32"
                theNumber = "0800 444 4444"
                Case Else
                    theNumber = "0800 222 2222"
        End Select
    Else
        Select Case Request.Cookies("myCookie2").Value
                Case "EZ12"
                theNumber = "0800 111 1111"
Case "ER34"
                theNumber = "0800 333 3333"
Case "RE32"
                theNumber = "0800 444 4444"
                Case Else
                    theNumber = "0800 222 2222"
        End Select
    End If

Basically the cases are always going to be the same but it will select the case from one of 2 places depending on whether myCookie has a value.

Since there may be quite a few cases is there anyway I can get away with only listing them once.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

溺ぐ爱和你が 2024-10-11 00:31:26

回答第二个编辑问题...

' pseudo code
If Not Request.Cookies("mycookie") is Nothing THen

   Select Case Request.Cookies("mycookie").Value
    Case "1234"

    Case "5678"

    Case Else
        '?

   End Select

End If

'再次回答第二个问题

Dim theNumber as String = "0800 222 2222"
If Not Request.Cookies("mycookie") is Nothing AndAlso Request.Cookies("mycookie").Value = "EZ12" Then
    theNumber = "0800 111 1111"
ElseIf Not Request.Cookies("mycookie2") is Nothing AndAlso Request.Cookies("mycookie2").Value = "EZ12" Then
    theNumber = "0800 111 1111"
End If

answering the second edit question...

' pseudo code
If Not Request.Cookies("mycookie") is Nothing THen

   Select Case Request.Cookies("mycookie").Value
    Case "1234"

    Case "5678"

    Case Else
        '?

   End Select

End If

' second question again

Dim theNumber as String = "0800 222 2222"
If Not Request.Cookies("mycookie") is Nothing AndAlso Request.Cookies("mycookie").Value = "EZ12" Then
    theNumber = "0800 111 1111"
ElseIf Not Request.Cookies("mycookie2") is Nothing AndAlso Request.Cookies("mycookie2").Value = "EZ12" Then
    theNumber = "0800 111 1111"
End If
≈。彩虹 2024-10-11 00:31:26

就像进行任何其他空检查一样进行操作:

If Not Request.Cookies("myCookie") Is Nothing Then
   // read value
End If

如果您经常这样做,您可能希望为 HTTP Cookie 创建一个包装器,其中 getter 内置了空检查。

Just do it as you do any other null check:

If Not Request.Cookies("myCookie") Is Nothing Then
   // read value
End If

If your doing this often you might like to create a wrapper for the HTTP Cookies, where the getter has the null check built in.

悲欢浪云 2024-10-11 00:31:26

你可以这样做:

If Request.Cookies["myCookie"] IsNot Nothing 
   And Request.Cookies["myCookie"].Value = "1234" Then ...

编辑:解决第二个问题:

var value = If(Request.Cookies("myCookie") IsNot Nothing, Request.Cookies("myCookie").Value & "", String.Empty);  
Select Case value
...
End Select

You could do it like this:

If Request.Cookies["myCookie"] IsNot Nothing 
   And Request.Cookies["myCookie"].Value = "1234" Then ...

EDIT: Addressing the second question:

var value = If(Request.Cookies("myCookie") IsNot Nothing, Request.Cookies("myCookie").Value & "", String.Empty);  
Select Case value
...
End Select
夏末的微笑 2024-10-11 00:31:26

if 语句的组成部分将从左到右进行检查。因此,如果您检查 cookie 是否存在,然后检查其值,就好像 cookie 不存在一样,第一次检查将失败,第二部分将不会被处理:

If Not Request.Cookies("myCookie") Is Nothing And Request.Cookies("myCookie").Value = "1234" Then
   // do stuff
End If

The components of an if statement will be checked from left to right. Therefore if you check that the cookie exists and then check its value you will be o.k. as if the cookie doesn't exist it will fail the first check and the second part will not be processed:

If Not Request.Cookies("myCookie") Is Nothing And Request.Cookies("myCookie").Value = "1234" Then
   // do stuff
End If
夏有森光若流苏 2024-10-11 00:31:26

0ne 方法是

If ((Request.Cookies("myCookie") IsNot Nothing) AndAlso (Request.Cookies("myCookie").Value = "1234")) Then
    'Do Stuff
End If

0ne way to do this is

If ((Request.Cookies("myCookie") IsNot Nothing) AndAlso (Request.Cookies("myCookie").Value = "1234")) Then
    'Do Stuff
End If
偏爱自由 2024-10-11 00:31:26

实现此目的的一种方法是

If ((Request.Cookies("myCookie") IsNot Nothing) AndAlso (Request.Cookies("myCookie").Value = "1234")) Then
    'Do Stuff
End If

与上面的主要区别是使用 AndAlso,这通过评估第一部分 x IsNot Nothing 来工作,如果这是 false 那么 if 将失败(就像此后的任何内容一样)总是会导致失败)。仅当第一部分为真时,它才会尝试评估第二部分。

这提供了不检查任何内容的能力,并且还稍微增强了性能,因为它只会在需要时评估进一步的参数。

Or 版本是 OrElse,这将停止评估第一部分是否为 true,因为在这种情况下它将始终返回 true。

另一种方法有点混乱,但您可以在 if 语句内使用 .Net 内联 if 函数

If (If(Request.Cookies("myCookie"), "") = "1234") Then
    'Do Something
End If

带有两个语句的内联 if 会检查第一个语句,如果没有则返回第二个语句(在本例中为空字符串)。如果第一项不是空的,则返回它。

One way to do this is

If ((Request.Cookies("myCookie") IsNot Nothing) AndAlso (Request.Cookies("myCookie").Value = "1234")) Then
    'Do Stuff
End If

The main difference to the above is the use of AndAlso, this works by evaluating the first part x IsNot Nothing and if this is false then the if will fail (as anything after this will always cause a fail). Only if the 1st section is true will it try to evaluate the second section.

This provides the ability to check for nothing and also a slight performance enhancement as it will only evaluate further parameters when required.

The Or version is OrElse, this will stop evaluating if the 1st section of this is true as it would always return true in this case.

Another way is a bit messy but you can use .Net inline if function inside the if statement

If (If(Request.Cookies("myCookie"), "") = "1234") Then
    'Do Something
End If

The inline if with two statements checks the first and if it is nothing returns the second (in this case a blank string). If the first item is not nothing then it returns it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文