asp.net模板引擎Razor調用外部方法用法執行個體

來源:互聯網
上載者:User

 首先使用Razor的步驟:讀取cshtml、解析cshtml同時指定cacheName。

而這個步驟是重複的,為了遵循DRY原則,將這段代碼封裝為一個RazorHelper()方法

1 2 3 4 5 6 7 8 9 10 11 public class RazorHelper { public static string ParseRazor(HttpContext context, string csHtmlVirtualPath, object model) { string fullPath = context.Server.MapPath(csHtmlVirtualPath); string cshtml = File.ReadAllText(fullPath); string cacheName = fullPath + File.GetLastWriteTime(fullPath); string html = Razor.Parse(cshtml,model,cacheName); return html; } }

如何在cshtml中用Razor調用外部方法

1. 首先在cshtml檔案引用test1和test2所在類的命名空間

1 2 3 4 5 6 7 8 9 10 11 12 @using WebTest1.RazorDemo;<!--test1和test2所在類的命名空間--> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> @RazorTest.test1()<br /> @RazorTest.test2() </body> </html>

2. 在一般處理常式中調用RazorHelper.ParseRazor(),將讀取到的cshtml檔案返回給客戶

1 2 3 4 5 6 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", null); context.Response.Write(html); }

為什麼要在cshtml檔案中調用方法呢?

先看一個繁瑣的,在cshtml中插入checkbox的處理

1. 一般處理常式

1 2 bool gender = true; string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", new { Gender = gender });

2. cshtml檔案中處理checkbox的checked狀態

<input type="checkbox" @(Model.Gender?"checked":"") />
<!--加括弧改變優先順序,否則編譯器會將點Model後面的運算式當字串處理-->

是不是很亂?處女座不能忍。

我們知道方法可以封裝一些重複代碼,調用方法讓cshtml頁面更簡潔。

舉個例子:

要在cshtml頁面插入一個checkbox。

1. 首先封裝一個CheckBox()方法

1 2 3 4 5 6 7 8 9 10 11 public static RawString CheckBox(string name, string id, bool isChecked) { StringBuilder sb = new StringBuilder(); sb.Append("<input type='checkbox' id='").Append(id).Append("' ").Append("name='").Append(name).Append("' "); if (isChecked) { sb.Append("checked"); } sb.Append("/>"); return new RawString(sb.ToString()); }

2. 在一般處理常式中讀取和解析cshtml檔案

 
1 2 string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", null); context.Response.Write(html);

3. 在cshtml檔案中調用CheckBox()方法,將checkbox插入cshtml

 
1 2 3 4 5 6 7 8 9 10 11 @using WebTest1.RazorDemo;<!--test1和test2所在類的命名空間--> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> @RazorTest.CheckBox("apple","apple",true) </body> </html>

聯繫我們

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