主頁(yè) > 知識(shí)庫(kù) > asp.net模板引擎Razor調(diào)用外部方法用法實(shí)例

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

熱門標(biāo)簽:百度競(jìng)價(jià)排名 阿里云 服務(wù)器配置 網(wǎng)站排名優(yōu)化 集中運(yùn)營(yíng)管理辦法 硅谷的囚徒呼叫中心 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 地方門戶網(wǎng)站

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

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

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

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調(diào)用外部方法

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

@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. 在一般處理程序中調(diào)用RazorHelper.ParseRazor(),將讀取到的cshtml文件返回給客戶

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

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

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

1. 一般處理程序

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

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

input type="checkbox" @(Model.Gender?"checked":"") />
!--加括號(hào)改變優(yōu)先級(jí),否則編譯器會(huì)將點(diǎn)Model后面的表達(dá)式當(dāng)字符串處理-->

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

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

舉個(gè)例子:

要在cshtml頁(yè)面插入一個(gè)checkbox。

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

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文件

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

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

@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>

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

您可能感興趣的文章:
  • 詳解ASP.NET Razor 語(yǔ)法
  • ASP.NET MVC重寫RazorViewEngine實(shí)現(xiàn)多主題切換
  • 詳解ASP.NET MVC 利用Razor引擎生成靜態(tài)頁(yè)
  • ASP.NET MVC4 Razor模板簡(jiǎn)易分頁(yè)效果
  • ASP.NET Razor模板引擎中輸出Html的兩種方式
  • ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)
  • asp.net模板引擎Razor中cacheName的問(wèn)題分析
  • 使用Asp.net Mvc3 Razor視圖方式擴(kuò)展JQuery UI Widgets方法介紹
  • 詳細(xì)分析ASP.NET Razor之C# 變量

標(biāo)簽:威海 烏蘭察布 西雙版納 隨州 梧州 廣西 甘孜 開封

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net模板引擎Razor調(diào)用外部方法用法實(shí)例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266