本文实例讲述了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状态
<input type="checkbox" @(Model.Gender"checked":"") />
<!--加括号改变优先级,否则编译器会将点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程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《古筝系列-弦凝指咽声停处》1CD[MP3][656.5MB]
- 群星.1994-大烂片2辑【派森】【WAV+CUE】
- 吴倩莲.1997-望爱【EMI百代】【WAV+CUE】
- 杨千嬅.2002-万紫千红演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郭采洁.2015-Begin.Again爱造飞鸡【华纳】【FLAC分轨】
- 许志安.2011-ON.AND.ON【东亚】【WAV+CUE】
- 潘秀琼.1994-木兰从军【名将】【WAV+CUE】
- 纯音入心系列纯音乐《古筝系列-当流行乐遇到古筝》1CD[MP3][1.9GB]
- 纯音入心系列纯音乐《精选古筝名曲100首》1CD[MP3][388.7MB]
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】