本文实例讲述了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;
@RazorTest.test1()
@RazorTest.test2()
2. 在一般处理程序中调用RazorHelper.ParseRazor(),将读取到的cshtml文件返回给客户
public void ProcessRequ