asp.net模板引擎Razor调用外部方法用法实例

所属分类: 网络编程 / ASP.NET 阅读数: 1070
收藏 0 赞 0 分享

本文实例讲述了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程序设计有所帮助。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多