asp.net的asp.net问题

时间:2010-11-25 11:03:44

标签: asp.net vb.net cookies null

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

以下是代码:

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

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

有人有任何想法吗?

编辑:

好的,让它运转起来,但是因为有很多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是否有值从2个地方中的一个选择案例。

由于可能存在相当多的情况,我只能将它们列出一次就可以逃脱。?

6 个答案:

答案 0 :(得分:2)

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

' 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

答案 1 :(得分:1)

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

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

如果你经常这样做,你可能想为HTTP Cookies创建一个包装器,其中getter内置了null检查。

答案 2 :(得分:1)

你可以这样做:

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

答案 3 :(得分:0)

将从左到右检查if语句的组件。因此,如果您检查cookie是否存在然后检查其值,那么您将是o.k.如果cookie不存在,它将无法通过第一次检查,第二部分将不会被处理:

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

答案 4 :(得分:0)

一种方法是

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

与上述的主要区别在于使用AndAlso,这通过评估第一部分x IsNot Nothing来工作,如果这是假的,则if将失败(因为此后的任何事情总是会导致失败)。只有当第1部分为真时,它才会尝试评估第二部分。

这提供了无需检查的能力以及轻微的性能增强,因为它只会在需要时评估更多参数。

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

另一种方式有点乱,但你可以在if语句

中使用.Net inline if函数
If (If(Request.Cookies("myCookie"), "") = "1234") Then
    'Do Something
End If

如果有2个语句的内联检查第一个,如果没有,则返回第二个(在这种情况下为空字符串)。如果第一项不是什么,则返回它。

希望这些解决方案有所帮助

答案 5 :(得分:0)

这样做的唯一方法是

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