本文实例讲述了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;
}
}
|
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程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 71
-
2025-05-29 87
-
2025-05-25 42
-
2025-05-25 87
-
2025-05-25 81
热门评论