国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net模板引擎Razor調(diào)用外部方法用法實例

2024-07-10 13:29:09
字體:
供稿:網(wǎng)友

這篇文章主要介紹了asp.net模板引擎Razor調(diào)用外部方法用法,實例分析了Razor調(diào)用外部方法的相關(guān)使用技巧,需要的朋友可以參考下

本文實例講述了asp.net模板引擎Razor調(diào)用外部方法用法。分享給大家供大家參考。具體如下:

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

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

 

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

如何在cshtml中用Razor調(diào)用外部方法

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

 

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

2. 在一般處理程序中調(diào)用RazorHelper.ParseRazor(),將讀取到的cshtml文件返回給客戶

 

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

為什么要在cshtml文件中調(diào)用方法呢?

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

1. 一般處理程序

 

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

2. cshtml文件中處理checkbox的checked狀態(tài)

 

  1. <input type="checkbox" @(Model.Gender?"checked":"") /> 
  2. <!--加括號改變優(yōu)先級,否則編譯器會將點Model后面的表達式當字符串處理--> 

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

我們知道方法可以封裝一些重復(fù)代碼,調(diào)用方法讓cshtml頁面更簡潔。

舉個例子:

要在cshtml頁面插入一個checkbox。

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

 

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

2. 在一般處理程序中讀取和解析cshtml文件

 

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

3. 在cshtml文件中調(diào)用CheckBox()方法,將checkbox插入cshtml

 

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

希望本文所述對大家的asp.net程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湾仔区| 伊吾县| 合水县| 海南省| 阿克苏市| 丰县| 建湖县| 讷河市| 阿克苏市| 攀枝花市| 泸定县| 响水县| 那曲县| 岑溪市| 樟树市| 平度市| 大同市| 五峰| 巴林左旗| 筠连县| 醴陵市| 贞丰县| 泾川县| 张家港市| 盐亭县| 观塘区| 永清县| 大田县| 蒙城县| 陈巴尔虎旗| 柯坪县| 陇南市| 乐昌市| 十堰市| 长海县| 潞城市| 云霄县| 福州市| 西乌| 天镇县| 称多县|