ASP.NET MVC4 Razor

來源:互聯網
上載者:User

標籤:http   io   os   使用   java   ar   strong   for   檔案   

1 Code Expressions  代碼錶達式

   @運算式  or  @(Expressions )

例如1: <h1>Listing @stuff.Length items.</h1   

Razor peeks at the next character and sees an angle bracket, which isn’t a valid identifier and transitions back into markup mode

例如2:

@{
string rootNamespace = "MyApp";
}
<span>@rootNamespace.Models</span>

What hope to output was  :

<span>MyApp.Models</span>

必須這樣寫:<span>@(rootNamespace).Models</span> ,通過括弧來標記文法結束。

 

example 3:

How to show an e-mail address:

<span>[email protected]</span>                      Razor 語法能智能識別 一個 正確的e-mail 地址

但是像微博這種,@名字    ,則需要轉義輸出:

<p>
You should follow
@haacked, @jongalloway, @bradwilson, @odetocode
</p>

You can do so by using @@ sing ,just like that:

<p>
You should follow
@@haacked, @@jongalloway, @@bradwilson, @@odetocode
</p>

 

2 HTML Encoding

為了跨網站的指令碼攻擊,Razor 語法會直接將指令碼代碼編碼輸出。

@{
string message = "<script>alert(‘haacked!‘);</script>";
}
<span>@message</span>

輸出:

<span>&lt;script&gt;alert(&#39;haacked!&#39;);&lt;/script&gt;</span>

此條實際測試時(chrome版本 37.0.2062.120 m,IE11)均顯示為: <span><script>alert(‘haacked!‘);</script></span>

如果需要原樣輸出 代碼文本 需要使用 @Html.Raw():

@{
string message = "<strong>This is bold!</strong>";
}
<span>@Html.Raw(message)</span>
輸出:
<span><strong>This is bold!</strong></span>

實際測試: 顯示為:This is bold!     當作html標記執行了!!!

如果 message ="<script>alert(‘haacked!‘);</script>"; 那麼 

<span>@Html.Raw(message)</span>  會彈出一個 haacked!  對話方塊,執行js指令碼!

 

可以在 Jquery 文法中使用 Razor文法:

<script type="text/javascript">
        $(function () {
            var message = ‘Hello @ViewBag.Title‘;
    $("#message").html(message).show(‘slow‘);
});
</script>


結果: Hello My Sample title

可以使用 @Ajax.JavaScriptStringEncode(string value)   對 字元內容進行編碼

 

3  Code Block  代碼塊

  @{ }                           代碼塊

例如:

@{
string s = "One line of code.";
ViewBag.Title "Another line of code";
}

  @foreach(){}   , @while() {} ;@for(){}             迴圈代碼塊

@if(){} ;  條件塊

@*      *@     註解區塊

例如:

@foreach (Mvc4App.Models.LoginModel login in ViewBag.LoginModels)
          {
               UserName:<li>@login.UserName</li>
          }

4 Layouts

用來呈現布局設計。@RenderBody()

例如: 主布局:SiteLayout.cshtml 檔案

<!DOCTYPE html>
<html>
<head><title>@ViewBag.Title</title></head>
<body>
The Razor View Engine x 67
<h1>@ViewBag.Title</h1>
<div id="main-content">@RenderBody()</div>
</body>
</html>

使用布局檔案 index.cshtml

@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
View.Title = "The Index!";
}
<p>This is the main content!</p>

---------------------------------------------

呈現 布局段   @RenderSection()

<!DOCTYPE html>
<html>
<head><title>@ViewBag.Title</title></head>
<body>
<h1>@ViewBag.Title</h1>
<div id="main-content">@RenderBody()</div>
<footer>@RenderSection("Footer")</footer>
</body>
</html>

更新index.cshtml

@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
View.Title = "The Index!";
}
<p>This is the main content!</p>
@section Footer {
This is the <strong>footer</strong>.
}

-------------------------------------------------------

如果有些需要呈現,有些不需要,可以使用RenderSection 的重載方法:

<footer>@RenderSection("Footer", required: false)</footer>

還可以判斷段是否存在

<footer>
@if (IsSectionDefined("Footer")) {
RenderSection("Footer");
}
else {
<span>This is the default footer.</span>
}
</footer>

------------------------------------------------------------

5  分布視圖 partial view

在HomeControl 中添加Action 

public ActionResult Partial()
      {
          ViewBag.Part = "this is partial view";
          return PartialView();
      }

右鍵添加視圖partial.cshtml,分布視圖不能使用Layout 屬性。

<div>
    <p>Hello @ViewBag.Part</p>
</div>

分布視圖與一般視圖可以一樣的方式瀏覽  http://localhost:46918/home/partial

分布視圖可以同Ajax 配合,直接載入:

  <input type="button" value="click me to load partial view" onclick="loadPartil()" />
    <div id="result"></div>
<script type="text/javascript">
    function loadPartil() {
        $(‘#result‘).load(‘/home/partial‘);
    }
</script>

ASP.NET MVC4 Razor

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.