Single If line output with Razor and .NET.

Razor doesn't look like my single-line If statements ... Usually in VB.NET I can write a single-line If statement like this:

 If True Then Dim x As Integer 

However, when I do the same with Razor in a .vbhtml file like this ...

 @If True Then Dim x As Integer 

I get an error

The If block has not been completed. All "If" statements must be completed using the appropriate "End If".

What gives? Why won't the razor take this? I understand that a simple solution is to just use a code block, for example

 @code If True Then Dim x As Integer End Code 

But that is not what I am looking for. I'm curious why Razor cannot recognize the VB.Net instruction of a single If line, using only @If... Any ideas?

+4
source share
3 answers

Same issue with C # as described here :

Razor uses code syntax to indent: Razor Parser code ends up reading opening and closing characters or HTML elements. As a result, the use of the holes "{" and closing "}" is mandatory, even for single-line instructions

You do business

 @code If True Then Dim x As Integer End Code 

You can do it like this, but you don't like it (inline)

 @If (True) Then Dim x As Integer End If 
+1
source

In VB.Net, you execute inline If Statement:

 @(If(boolCondition, "truePart", "falsePart")) 
+5
source

I'm not sure about VB, but it works in C #.

 @{if (true) { int x; }} 
0
source

Source: https://habr.com/ru/post/1498056/


All Articles