Razor入門

來源:互聯網
上載者:User

標籤:

一、Razor簡介
Razor不是程式設計語言,它是一種允許您向網頁中嵌入基於伺服器的代碼的標記文法,也就是可以在html網頁中嵌入的寫入C#代碼,Razor在VS中有自動提示,使用起來會方便一點,如下代碼,迴圈輸出i的值

<ul>
  @{
    for(int i=0;i<10;i++)
    {
      <li>@i</li>
    }
  }
</ul>

當網頁被寫入瀏覽器時,基於伺服器的代碼能夠建立動態內容。在網頁載入時,伺服器在向瀏覽器返回頁面之前,會執行頁面內的基於伺服器代碼。由於是在伺服器上運行,這種代碼能執行複雜的任務,比如訪問資料庫。
Razor基於ASP.NET,它為web應用程式的建立而設計。它擁有傳統 ASP.NET 標記的能力,但更易使用,也更易學習。

二、Razor工作流程
1.Razor讀取cshtml檔案(string cshtml = Razor.Parse(html, new { Name = name, Age = age });)
2.Razor會根據cshtml內容動態產生C#代碼
3.執行C#代碼形成一個程式集
4.通過反射的方式執行程式集形成html代碼

三、Razor使用方法(用ashx一般處理常式輸出cshtml內容)
因為Razor大多是在MVC中使用,要想脫離MVC使用Razor,可以藉助開源的RazorEngine.dll,就可以在控制台、winform表單、ASP.NET中使用。
1.引用RazorEngine.dll並匯入命名空間using RazorEngine;
//讀取檔案路徑
string fillPath = context.Server.MapPath("~/Text1.cshtml");
//讀取檔案內容
string html = File.ReadAllText(fillPath);
//調用Razor的方法Parse
string cshtml = Razor.Parse(html, new { Name = name, Age = age });
//輸出
context.Response.Write(cshtml);
2.在cshtml中調用伺服器的變數
1)首先在伺服器中將變數定義為全域靜態變數,如:
public static string name;
public static List<string> list = new List<string>();
2)然後在伺服器的ProcessRequest方法中設定變數的值,如:
name="abcd";
list.RemoveRange(0, list.Count);//先清空,否則在用戶端擷取時會重複出現
list.Add("aaa");list.Add("bbb");list.Add("ccc");
3)最後在cshtml擷取變數值時,先最頂部匯入命名空間,再擷取,如:
@using WebApplication2 //匯入變數所在的命名空間,前面加@,寫在最頂部
@{string str = Text1.name;} //接收變數值,用@{...},變數值寫在大括弧裡面,變數名前面需要加上類名
//那麼value="@str"就是將變數的值寫入文字框中
<input type="text" name="name" value="@str" />
//如下為接收List集合

@{
  List<string> listCount = Text1.list;
}
//輸出list值
for(int i=0;i<listCount.Count;i++)
{
  <li>@listCount[i]</li>
}

//擷取類中的屬性值
C#中代碼:
public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

string name = "syfpc";
int age = 8;
string cshtml = Razor.Parse(html, new { Name = name, Age = age });

cshtml代碼:
@Model.Name
@Model.Age

//封裝一個ParseRazor方法

public static string ParseRazor(HttpContext context, string cshtmlPath, object model){    string fullPath = context.Server.MapPath(cshtmlPath);    string cshtml = File.ReadAllText(fullPath);    string cachName = fullPath + File.GetLastWriteTime(fullPath);    string html = Razor.Parse(cshtml, model, cachName);    return html;}

//產生一個文字框

public static RawString Text2(){    RawString rawstring = new RawString("<input type=‘text‘ />");    return rawstring;}

//在cshtml中調用此方法
@ProccessHelp.Text2()

//產生一個選項按鈕

public static RawString CheckBox(string name, string id, bool ischecked){    StringBuilder sb = new StringBuilder();    sb.Append("<input type=‘checkbox‘ name=‘" + name + "‘ id=‘" + id + "‘");    if (ischecked)    {        sb.Append("checked=‘checked‘");    }    sb.Append("/>");    RawString rawstring = new RawString(sb.ToString());    return rawstring;}

//在cshtml中調用此方法
@ProccessHelp.CheckBox("check","check",true)

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.