使用Razor和.NET的一行If语句 [英] One line If statement with Razor and .NET
问题描述
Razor似乎不喜欢我的一行如果
语句...通常在VB.NET中,我可以写一行如果
语句如下:
Razor doesn't seem to like my one line If
statements... Normally in VB.NET, I can write a one line If
statement like so:
If True Then Dim x As Integer
然而,当我在这样的 .vbhtml
文件中对Razor做同样的事情时...
However, when I do the same with Razor in a .vbhtml
file like this...
@If True Then Dim x As Integer
我收到错误
If块不是终止。所有If语句必须以匹配的End If终止。
给出了什么? Razor为什么不接受这个?我意识到一个简单的解决方案是只使用代码块,如
What gives? Why won't Razor take this? I realize that a simple solution is to just use a code block like
@code
If True Then Dim x As Integer
End Code
但这不是我想要的。我很好奇为什么Razor无法识别VB.Net一行如果
语句仅使用 @If ...
。有什么想法?
But thats not what I'm looking for. I'm curious why Razor can't recognize a VB.Net one line If
statement using just @If...
. Any ideas?
推荐答案
与C#相同的问题,正如所解释的这里:
Same problem with C#, As explained here :
Razor使用代码语法来推断缩进: Razor Parser通过读取开始和结束字符或HTML
元素来推断代码
。因此,使用开口{和关闭}是强制性的
,即使对于单行指令也是如此
Razor uses code syntax to infer indent: Razor Parser infers code ending by reading the opening and the closing characters or HTML elements. In consequence, the use of openings "{" and closings "}" is mandatory, even for single line instructions
你做的是怀特的事情
@code
If True Then Dim x As Integer
End Code
你可以这样做,但不是你喜欢的(内联)
You can do it like that but it's not you like (inline)
@If (True) Then
Dim x As Integer
End If
这篇关于使用Razor和.NET的一行If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!