Original article: razor view engine-basic syntax
All codes starting with @ or @ {/* Code body */} (no spaces can be added directly to @ and {) will be processed by the ASP. NET engine.
Each line of code in @ {/* Code body */} must end with ";", as shown in
@{
VaR I = 10;
Var y = 20;
}
@ Xxx does not need to end with ";", as shown in figure
@ I output 10
@ Y; output 20;
Uppercase/lowercase letters in the code area.
Character Type constants must be enclosed by "", for example, @ {string STR = "my string ";}
-Note-
To output the "@" character on the page
You can use html ascii Encoding & #64;
Of course, Razor also provides the smart analysis function: if the first character of @ is not a blank character, ASP. NET will not process it.
For example, <p> [email protected] XX </P> outputs [email protected] xx
Single Row Syntax:
@ {Var I = 10 ;}
Multiline Syntax:
@{
var I = 10;
Var y = 20;
}
1. When using local variables, Razor does not support access modifiers (public, private, etc.), which makes no sense)
Define local variables on a single row
@ {Var Total = 7 ;}
@ {Var mymessage = "Hello World ";}
Define local variables on multiple rows
@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Today is: " + weekDay;
}
Use variables in the context
<p>The value of your account is: @total </p>
<p>The value of myMessage is: @myMessage</p>
Note:Variable concatenation output
@ {Var I = 10 ;}
<P> text @ I text </P>
But what if you want to output text10text?
<P> [email protected] {@ I} text </P>
<P> [email protected] Text </P>: [email protected] Text
<P> [email protected] </P> [email protected]
<P> text @ itext </P>
If the method name of the output variable does not need to be enclosed by @ {}, it can also take effect. However, remember to add a space before the @ character (thanks to spook for pointing out), for example:
<P> text @ I. tostring () text </P>
You can directly write the variable object: @ var1 @ var2 @ myobject. xx
2. Use logical processing
@{
if (xx)
{
//do something
}
else
{
//do anything
}
}
3. Use HTML tags inside @ {...}
@{
<p>text</P>
<div>div1</div>
}
4. output text inside @ {...}
Use @: to output a single line:
@{
@:This is some text
@:This is text too
@: @ I can also output variables
}
Use <text/> to output multiple rows:
@{
<text>
tomorrow is good
some girl is nice
</text>
}
5. Use comments inside @ {...}
@{
// Single line comment
var i = 10;
//defg
}
@ * Multi-line comment *@
@*
Multi-line comment
Multi-line comment
*@
@{
@*
Multi-line comment
Multi-line comment
*@
var i = 10; @* asdfasf *@
}
<! -- You can also use C # default/*... */-->
@{
/*
Multi-line comment
*/
}
If it is used inside @ {...} <! --> Comment, it is output to the page. If it is in <! --> If @ variable is used internally, it will be processed.
@{
<! -- Time now: @ datetime. Now. tostring () -->
}
Output: <! -- Time now: 4/9/2011 -->
6. type conversion
Asint (), isint ()
Asbool (), isbool ()
Asfloat (), isfloat ()
Asdecimal (), isdecimal ()
Asdatetime (), isdatetime ()
Tostring ()
Example:
@{
var i = “10”;
}
<P> I = @ I. asint () </P> <! -- Output I = 10 -->
7. Use loop
<! -- Method 1 -->
@for (int i = 10; i < 11; i++)
{
@:@i
}
<! -- Method 2 -->
@{
for (int i = 10; i < 11; i++)
{
//do something
}
}
<! -- While similarly -->