ASP. NET MVC3 Series Tutorial-Razor view engine basic syntax

Source: Internet
Author: User
Tags define local

Http://www.cnblogs.com/highend/archive/2011/04/09/aspnet_mvc3_razor_engine.html

4. All documents that begin with "_"

ASP. MVC3 By default, all _*.cshtml documents that have access addresses that begin with "_" are rejected. With regard to _*.cshtml documentation, he is a part of webpages, which will be described in detail later with the instructions for using the "_" document. For example, Access http://localhost:7985/_ViewPage1.cshtml

Ii:razor View Engine-basic syntax

Infrastructure

Any part of the code that begins with @ or @{/* code body */} (at @ and {You must not add any spaces directly) will be processed by the ASP. Each line of code in the @{/* Code body */} must be ";"     End, such as @{var i = 10; var y = 20; And @xxx do not need to ";" As a terminator, such as @i output of ten @y; Output 20; The letters in the code area are case-sensitive. Character type constants must be enclosed in "" For example: @{string str = "my string";} -note-

You can use HTML ASCII encoding & #64 If you want the "@" character in the page output; Of course Razor also provides intelligent analysis capabilities: If the previous word of @ characters is a non-whitespace character, then ASP. NET will not process it such as: <p>[email protected] xx</p> output [email protected] XX
Single-line syntax: @{var I = 10;} Multiline Syntax:

    var I = 10;
    Var y = 20;
}

1. Using local variables, razor does not support the access modifier (public,private, etc., which does not make any sense) to define local variables @{var total = 7 on a single line,} @{var mymessage = "Hello World";} define office on multiple lines Part variable

@{
    var greeting = "Welcome to our site!";
    var weekDay = DateTime.Now.DayOfWeek;
    var greetingmessage = greeting + "Today is:" + weekDay;
}

Using Variables in context

<p</P>   
<p>the value of Mymessage is: @myMessage</P>    

Note : variable splicing output @{var i = ten;} < P>text @i text</p> will output text, text, but what if you want to output text10text?

<p>[email protected]{@i}text</P>    
<p>[email protected] text</P> will output [email protected] text    
<p>[email protected]</P> will output [email protected]    
<p>text @itext</P> will error    

If the output is a variable method name does not need to be surrounded by @{} can also take effect, but note in the @ character before the space (thank Spook pointed) such as: <p>text @i.tostring () text</p> use variable object can be directly written: @ Var1 @var2 @myObject. XX

2. Using logical processing

@{
    if (XX)
    {
    Do something
    }
    Else
    {
    Do anything
    }
}

3. In @{...} Using HTML tags internally

@{
    <p>text</P>    
    <div>div1</div>    
}

4. In @{...} Internal output text using @: For single-line output:

@{
    @:this is some text
    @:this is text too
    @:@i can also output variables
}

Multi-line output with <text/>:

@{
    <text> 
        Tomorrow is good
        Some girl is nice
    </text> 
}

5. In @{...} Internal Use Notes

@{
    Single-line Comment
    var i = 10;
    Defg
}
    @* Multi-line Comment *@
    
        Multi-line comments
        
    *@
@{
    @*
        Multi-line comments
        
    *@
    var i = ten;  @* asdfasf *@
}
<!--can also use the C # default */* ...
@{
    /*
        
    */
}

If in @{...} Internal use <!----> comments are output to the page, and if the @ variable is used inside <!---->, it will be processed @{<!--time now: @DateTime. now.tostring ()--Output: <!--time now:4/9/2011 12:01-->>

6. Type conversion asint (), Isint () Asbool (), Isbool () asfloat (), Isfloat () Asdecimal (), Isdecimal () asdatetime (), Isdatetime () ToString () Example:

@{
    var i = "10";
}
<p</p    

7. Using loops

<!--Way 1-->
< 11; i++)
{
    @:@i
}
<!--Way 2-->
@{
    < 11; i++)
    {
        Do something
    }
}
<!--while empathy--

ASP. NET MVC3 Series Tutorial-Razor view engine basic syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.