Razor視圖引擎-基礎文法

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   

原文:Razor視圖引擎-基礎文法

所有以 @開頭 或 @{ /* 代碼體 */ }  (在@與{直接不得添加任何空格) 的部分代碼都會被ASP.NET引擎進行處理.
在 @{ /*代碼體*/ } 內的代碼每一行都必須以";"結束,如
@{
    var i = 10;
    var y = 20;
}
而 @xxx 則不需要以";"作為結束符,如
@i 輸出 10
@y; 輸出 20;
代碼區內字母分大小寫.
字元類型常量必須用""括起例如: @{ string str = "my string"; }
-注意-

如需要在頁面輸出”@”字元
可以使用HTML ASCII編碼@
當然Razor也提供智能分析功能: 如果在@的前一個字元若是非空白字元,則ASP.NET不會對其進行處理
如:<p>[email protected] xx</p> 輸出 [email protected] xx

單行文法:
@{ var I = 10; }
多行文法:

@{ 
    var I = 10;
    Var y = 20;
}

1. 使用局部變數,Razor不支援存取修飾詞(public,private等,這個沒任何意義)
在單行上定義局部變數
@{ var total = 7; }
@{ var myMessage = "Hello World";}
在多行上定義局部變數

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


在上下文中使用變數

<p>The value of your account is: @total </p>
<p>The value of myMessage is: @myMessage</p>


注意:
變數拼接輸出
@{ var i = 10; }
<p>text @i text</p> 將輸出 text 10 text
但是如果你想要輸出 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> 將報錯

如果是輸出的是變數的方法名則不需要用@{}括住也可生效,但注意在@字元前記得加空格(感謝spook指出)如:
<p>text @i.ToString()text</p>
使用變數對象可直接寫: @var1 @var2 @myObject.xx

2. 使用邏輯處理

@{
    if (xx)
    {
    //do something
    }
    else
    {
    //do anything
    }
}

3. 在@{... }內部使用html標記

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

4. 在@{...}內部輸出文本
利用@:進行單行輸出:

@{
    @:This is some text
    @:This is text too
    @:@i 也可輸出變數
}

利用<text />進行多行輸出:

@{
<text>
        tomorrow is good
        some girl is nice
</text>
}

5. 在@{...}內部使用注釋

@{
    //單行注釋
    var i = 10;
    //defg
}
 
    @* 多行注釋 *@
    @* 
        多行注釋
        多行注釋 
    *@
 
 
@{
    @*
        多行注釋
        多行注釋 
    *@
    var i = 10;  @* asdfasf *@
}
 
<!-- 同時也可以使用C#預設的/* ... */ -->
 
@{
    /*
        多行注釋 
    */
}

若在@{ ... }內部使用<!-- -->注釋,則會輸出到頁面之中,如果在<!-- -->內部使用@變數,則會被處理
@{
<!-- time now: @DateTime.Now.ToString() -->
}
輸出: <!-- time now: 4/9/2011 12:01 -->>

6. 類型轉換
AsInt(), IsInt()
AsBool(),IsBool()
AsFloat(),IsFloat()
AsDecimal(),IsDecimal()
AsDateTime(),IsDateTime()
ToString()
例子:

@{
    var i = “10”;
}
 
<p> i = @i.AsInt() </p><!-- 輸出 i = 10 -->

7. 使用迴圈

<!--方式1-->
@for (int i = 10; i < 11; i++)
{
    @:@i
}
<!--方式2-->
@{
    for (int i = 10; i < 11; i++)
    {
        //do something
    }
}
 
<!--while同理-->

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.