NVelocity 和Razor 模板引擎小Demo, WebForm模板引擎求教
2012-02-22 17:34
423 查看
我想用asp.net webform 做个模板引擎的demo,目前用NVelocity 和Razor 各做了个小demo。
首先来看下NVelocity。
加载和显示模板
1 public partial class ParseRazor : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 //模板文件虚拟目录
6 var path = System.IO.Path.Combine(Request.ApplicationPath, "/Template/demo.cshtml");
7 var type = BuildManager.GetCompiledType(path);
8 var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
9
10
11 var model = Article.GetArticleList();
12 string searchID = Request.Form["searchID"];
13 int id = 0;
14
15 var stringWriter = new StringWriter();
16 if (searchID != null && int.TryParse(searchID, out id)) {
17 script.ExecutePageHierarchy(new WebPageContext(
18 new HttpContextWrapper(HttpContext.Current), null, model.Where(p => p.ID == id).ToList()
19 ), stringWriter);
20 }
21 else {
22
23 script.ExecutePageHierarchy(new WebPageContext(
24 new HttpContextWrapper(HttpContext.Current), null, model
25 ),stringWriter );
26
27 }
28
29 Response.Write(stringWriter);
30
31 }
32
33 }
像NVelocity的话可以通过 context.Put() 方法来输出多个变量,但使用Razor的话,如何添加多个变量在前台显示呢?
下面想试试 webform风格的模板,用WebForm自身的引擎。
但还不知道如何下手,大家能否给点建议?
源代码
首先来看下NVelocity。
加载和显示模板
1 public partial class ParseRazor : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 //模板文件虚拟目录
6 var path = System.IO.Path.Combine(Request.ApplicationPath, "/Template/demo.cshtml");
7 var type = BuildManager.GetCompiledType(path);
8 var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
9
10
11 var model = Article.GetArticleList();
12 string searchID = Request.Form["searchID"];
13 int id = 0;
14
15 var stringWriter = new StringWriter();
16 if (searchID != null && int.TryParse(searchID, out id)) {
17 script.ExecutePageHierarchy(new WebPageContext(
18 new HttpContextWrapper(HttpContext.Current), null, model.Where(p => p.ID == id).ToList()
19 ), stringWriter);
20 }
21 else {
22
23 script.ExecutePageHierarchy(new WebPageContext(
24 new HttpContextWrapper(HttpContext.Current), null, model
25 ),stringWriter );
26
27 }
28
29 Response.Write(stringWriter);
30
31 }
32
33 }
像NVelocity的话可以通过 context.Put() 方法来输出多个变量,但使用Razor的话,如何添加多个变量在前台显示呢?
下面想试试 webform风格的模板,用WebForm自身的引擎。
但还不知道如何下手,大家能否给点建议?
源代码
相关文章推荐
- Razor模板引擎
- nvelocity模板引擎
- C#模板引擎 RazorEngine3.7的简单使用
- .NET Core中使用Razor模板引擎
- 聊聊使用NVelocity模板引擎来开发代码生成工具
- MVC3 Razor模板引擎
- MVC3 Razor模板引擎
- 在winform里面如何使用razor模板引擎?
- 理解模板引擎Razor 的原理
- 模板引擎-NVelocity
- Razor模板引擎
- NVelocity是Java模板引擎Velocity的.Net版本
- 模板引擎-NVelocity
- 【搬运外国同行文章】在非网页程序里使用razor模板引擎
- NVelocity模板引擎
- 使用template (模板引擎写的demo)
- C#模板引擎 RazorEngine3.7的扩展 — 引入外部文件
- WebPart中使用Nvelocity模板引擎来进行呈现
- ASP.NET MVC Tip #19 – 使用 nVelocity 视图模板引擎
- 怎么在ASP.NET WebForm中使用Razor视图引擎(转载)