【发布时间】:2012-01-05 09:35:29
【问题描述】:
我刚开始使用 Razor 而不是 WebForms-ViewEngine。现在在我的 Razor-View 中我有这样的东西:
@{
int i = 42;
string text;
if (i == 42)
{
text = "i is 42!";
}
else //i is not 42 //<- Error here
{
text = "i is something else";
}
}
我收到警告,但在运行时它在 else 行中出现异常:
应为“{”,但找到了“/”。块语句必须包含在“{”和“}”中。您不能在 CSHTML 页面中使用单语句控制流语句。
显然编译器不喜欢 else 和 { 之间的 cmets。我还尝试使用 @* 和 /* 进行评论,这给出了类似的错误消息。
有没有像我想要的那样在剃须刀上发表评论?
免责声明:
是的,我知道我可以像这样简单地修复它:
@{
int i = 42;
string text;
if (i == 42)
{
text = "i is 42!";
}
else
{ //i is not 42
text = "i is something else";
}
}
但是它不符合我们的编码指南,并且在同一行添加注释使我的意图更加清晰。
【问题讨论】:
-
您的编码指南要求您对每个
else语句添加注释,其中包含对前面if的否定?你在if ... else if ... else if ... else链中做什么? -
不,就在最后一个 else 当 if 检查值列表时,将来可能会扩展。例如
if(val == Enum.Val1){...} else if(val == Enum.Val2){...} else //must be Enum.Val3, though Val4 might be added in the future
标签: asp.net .net asp.net-mvc asp.net-mvc-3 razor