带有多个or条件的C#if语句未返回预期行为

带有多个or条件的C#if语句未返回预期行为,c#,if-statement,conditional,conditional-formatting,C#,If Statement,Conditional,Conditional Formatting,我有一个if语句,它将在以下条件下显示.CSHTML布局: @if ((ViewBag.title != "Log in") || (ViewBag.title != "Register") || (ViewBag.title != "Confirm Email") || (ViewBag.title != "Login Failure") || (ViewBag.title != "Forgot your password?") || (View

我有一个if语句,它将在以下条件下显示.CSHTML布局:

    @if ((ViewBag.title != "Log in")
    || (ViewBag.title != "Register")
    || (ViewBag.title != "Confirm Email")
    || (ViewBag.title != "Login Failure")
    || (ViewBag.title != "Forgot your password?")
    || (ViewBag.title != "Forgot Password Confirmation")
    || (ViewBag.title != "Reset password")
    || (ViewBag.title != "Reset password confirmation")
    || (ViewBag.title != "Send")
    || (ViewBag.title != "Verify"))
{ Layout markup }
当我加载
登录
页面时;但是,将显示布局模板。设置断点表明页面标题与
!=“登录”
条件,并且不会引发异常。为了确定,我在中对照解决方案检查了我的标记,它看起来很好。。。不知怎么搞砸了我的陈述逻辑却不明白



您想要的是
&&
,而不是
|
。您的逻辑错误,您的条件将始终为真。

您的条件的计算结果始终为
true
。考虑以下条件:

if(value != "A" || value != "B")
它总是
true
,因为
value
不能同时等于
A
B

你要找的是
&&

@if ((ViewBag.title != "Log in")
&& (ViewBag.title != "Register")
&& (ViewBag.title != "Confirm Email")
... )
{ Layout markup }

使用&&opreator,在当前状态下,您的条件始终为true。

当标题为
登录时,第一个条件为false。所有其他条件均为真,因此设置了布局。你应该用ands替换ors。我们没有建立“始终正确”的连接。对我来说应该很明显但我正在学习。。。非常感谢。