[英]Why does the if work only sometimes?
I have this code in my project for checking the controller and the action in my .cshtml-file: 我的项目中有以下代码,用于检查控制器和.cshtml文件中的操作:
@if (ViewContext.RouteData.Values["controller"] == "Home" && ViewContext.RouteData.Values["action"] == "Index")
{
//Some html
}
When I open the page at the root ( http://localhost/ ), it shows the given html. 当我从根目录( http:// localhost / )打开页面时,它显示给定的html。 But when I open it with the full link ( http://localhost/Home/Index ) it's not showing up. 但是,当我使用完整链接( http:// localhost / Home / Index )打开它时,它没有显示出来。
When put the variables in the html of the webpage, they are exactly the same. 将变量放在网页的html中时,它们是完全相同的。
Why isn't this working then? 那为什么不起作用呢?
Use .Equals("")
instead of ==
. 使用.Equals("")
代替==
。
ViewContext.RouteData.Values["controller"]
is of type object
. ViewContext.RouteData.Values["controller"]
的类型为object
。 If you want to check the equality of the content, you first need to resolve the underlying type as String
. 如果要检查内容的相等性,则首先需要将基础类型解析为String
。 If you use ==
, then C# doesn't resolve the underlying type before doing the comparison. 如果使用==
,则C#在进行比较之前不会解析基础类型。 When you use Equals()
, C# does resolve the underlying type first. 当您使用Equals()
,C#会首先解析基础类型。 Here's why C# needs to know the underlying type. 这就是C#需要知道基础类型的原因。
When you use the ==
operator, and one of the operands is an object
, the check is for reference equality. 当使用==
运算符,并且其中一个操作数是object
,检查是否为引用相等。 In your question, it doesn't matter that underlying type is String
, because C# doesn't check the underlying type. 在您的问题中,基础类型为String
无关紧要,因为C#不会检查基础类型。 If it did check the underlying type, then the ==
would work, but it doesn't. 如果确实检查了基础类型,则==
将起作用,但不会。 It just checks whether the two references are the same. 它只是检查两个引用是否相同。 See MSDN . 参见MSDN 。
If you really want to use ==
, then you can tell C# explicitly to use a string
comparison. 如果您确实想使用==
,则可以明确告诉C#使用string
比较。
ViewContext.RouteData.Values["controller"].ToString()
(ViewContext.RouteData.Values["controller"] as string)
((string)ViewContext.RouteData.Values["controller"])
It's probably best just to use the Equals(string)
method. 最好只使用Equals(string)
方法。 C# will figure out on its own that you're working with a String
. C#会自己弄清楚您正在使用String
。 In other words, it does matter that the underlying type is a String
, because C# resolves the underlying type before calling any method on an object
. 换句话说,基础类型是String
确实很重要,因为C#在调用object
任何方法之前先解析基础类型。 And the comparison will be for content not reference equality, because the underlying type is String
. 并且比较将针对内容而不是引用相等,因为基础类型为String
。 See MSDN . 参见MSDN 。
Here is a Fiddle for you. 这是给您的小提琴 。
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
@ViewContext.RouteData.Values["controller"]
@ViewContext.RouteData.Values["action"]
@if (ViewContext.RouteData.Values["controller"].Equals("Home")
&& ViewContext.RouteData.Values["action"].Equals("Index"))
{
<p>True</p>
}
else
{
<p>False</p>
}
@if (ViewContext.RouteData.Values["controller"] == "Home"
&& ViewContext.RouteData.Values["action"] == "Index")
{
<p>True</p>
}
else
{
<p>False</p>
}
</body>
</html>
I suggest revert it like this, in case of null 我建议像这样将其还原为null
@if ("Home".Equals(ViewContext.RouteData.Values["controller"]) && "Index".Equals(ViewContext.RouteData.Values["action"]))
{
//Some html
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.