asp.net problem with null cookie
I'm hav开发者_如何学运维ing 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.?
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
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.
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
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
0ne way to do this is
If ((Request.Cookies("myCookie") IsNot Nothing) AndAlso (Request.Cookies("myCookie").Value = "1234")) Then
'Do Stuff
End 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.
精彩评论