激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - asp.net模板引擎Razor調用外部方法用法實例

asp.net模板引擎Razor調用外部方法用法實例

2019-12-17 13:08IT金鐘國 ASP.NET教程

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

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

首先使用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>

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美精品videos| 在线免费观看欧美 | 永久免费不卡在线观看黄网站 | 草逼一区| 玩偶姐姐 在线观看 | 蜜桃视频在线免费播放 | 久久久国产视频 | 国产亚洲精品久久久闺蜜 | 国产一区二区精品免费 | 久久电影一区二区 | 西川av在线一区二区三区 | 日本视频网 | 国产精品亚洲欧美 | 色婷婷av一区二区三区久久 | 欧美精品一二三区 | sm高h视频| 欧美一级爱爱 | 欧美精品成人一区二区三区四区 | 亚洲精品一区二区三区大胸 | 精品亚洲福利一区二区 | 国产一级毛片在线看 | 国产黄色录像片 | 欧美一级做a | 激情综合网俺也去 | 免费黄色在线观看网站 | 一区二区三区视频在线观看 | 国产精品剧情一区二区三区 | 亚洲人成网在线观看 | 在线成人www免费观看视频 | 九九热免费在线观看 | 免费视频一区 | 成年性羞羞视频免费观看无限 | 黄色大片免费网站 | 久久综合久久美利坚合众国 | 有色视频在线观看 | 黄色午夜剧场 | 粉嫩粉嫩一区二区三区在线播放 | 欧美一级全黄 | 4480午夜| 久草在线综合 | 久色网站 |