asp.net模板引擎Razor調用外部方法用法實例
本文實例講述了asp.net模板引擎Razor調用外部方法用法。分享給大家供大家參考。具體如下:
首先使用Razor的步驟:讀取cshtml、解析cshtml同時指定cacheName。
而這個步驟是重復的,為了遵循DRY原則,將這段代碼封裝為一個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調用外部方法
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. 在一般處理程序中調用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文件中調用方法呢?
先看一個繁瑣的,在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":"") />
<!--加括號改變優(yōu)先級,否則編譯器會將點Model后面的表達式當字符串處理-->
是不是很亂?處女座不能忍。
我們知道方法可以封裝一些重復代碼,調用方法讓cshtml頁面更簡潔。
舉個例子:
要在cshtml頁面插入一個checkbox。
1. 首先封裝一個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文件中調用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>
希望本文所述對大家的asp.net程序設計有所幫助。
相關文章
asp.net Silverlight應用程序中獲取載體aspx頁面參數
有時候SL應用中需要使用由aspx頁面中傳遞過來的參數值,此時通常有兩種方法獲取2009-11-11
ASP.NET MVC中異常Exception攔截的深入理解
異常信息的處理在程序中非常重要, 在asp.net mvc中提供異常屬性攔截器進行對異常信息的處理,下面這篇文章主要給大家介紹了關于ASP.NET MVC中異常Exception攔截的相關資料,需要的朋友可以參考下2018-07-07
ajaxToolkit:ModalPopupExtender演示及實現代碼
ajaxToolkit:ModalPopupExtender可以讓用戶模擬新開一個窗口,就是在模擬新開窗口作多項選項的功能,感興趣的朋友可以了解下,希望此文對你有所幫助2013-01-01
詳解ASP.NET Core和ASP.NET Framework共享身份驗證
本篇文章主要介紹了詳解ASP.NET Core和ASP.NET Framework共享身份驗證 ,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
ASP.NET Web應用程序出現Maximum request length
ASP.NET Web應用中導出數據時出現500-Internal Server Error,原因是客戶端請求長度超過了服務器配置的最大限制,解決方法在web.config增加maxRequestLength屬性,單位為字節(jié)(Byte),本文介紹ASP.NET Web應用程序出現Maximum request length exceeded報錯的原因,一起看看吧2024-12-12

