ASP.NET 的 null cookie 问题
我在编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
回答第二个编辑问题...
'再次回答第二个问题
answering the second edit question...
' second question again
就像进行任何其他空检查一样进行操作:
如果您经常这样做,您可能希望为 HTTP Cookie 创建一个包装器,其中 getter 内置了空检查。
Just do it as you do any other null check:
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:
EDIT: Addressing the second question:
if 语句的组成部分将从左到右进行检查。因此,如果您检查 cookie 是否存在,然后检查其值,就好像 cookie 不存在一样,第一次检查将失败,第二部分将不会被处理:
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:
0ne 方法是
0ne way to do this is
实现此目的的一种方法是
与上面的主要区别是使用 AndAlso,这通过评估第一部分 x IsNot Nothing 来工作,如果这是 false 那么 if 将失败(就像此后的任何内容一样)总是会导致失败)。仅当第一部分为真时,它才会尝试评估第二部分。
这提供了不检查任何内容的能力,并且还稍微增强了性能,因为它只会在需要时评估进一步的参数。
Or
版本是OrElse
,这将停止评估第一部分是否为 true,因为在这种情况下它将始终返回 true。另一种方法有点混乱,但您可以在 if 语句内使用 .Net 内联 if 函数
带有两个语句的内联 if 会检查第一个语句,如果没有则返回第二个语句(在本例中为空字符串)。如果第一项不是空的,则返回它。
One way to do this is
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 isOrElse
, 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
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.