为ASP.NET MVC 2.0添加Razor模板引擎 (on .NET4)
来源:互联网 发布:徐州淘宝村 编辑:程序博客网 时间:2024/03/28 17:30
@{var i = 11;}@(i+1)<br>@if (i%2==1){<p>truep>}else{<p>falsep>}输出结果为:
12<br> <p>truep>
在不久之后Ms还会对此提供Visual Studio 高亮及智能感知支持。
这种模板如此简捷,如果能用在现有的ASP.NET MVC 2.0上做为一个模板引擎是不错的。
首先我们要安装ASP.NET Web Pages,下载地址:http://bbs.eice.com.cn/showtopic-409.aspx ,当然直接安装WebMatrix也是可以。
安装之后在IIS中就会添加对cshtml及vbhtml的支持。
安装后程序集文件会被复制到Program Files/Microsoft ASP.NET/ASP.NET Web Pages/v1.0/Assemblies目录下。
其中包括
Microsoft.Data.dllMicrosoft.Web.Infrastructure.dllMicrosoft.Webpages.Compilation.dllMicrosoft.Webpages.Configuration.dllMicrosoft.Webpages.dllMicrosoft.Webpages.Helpers.dllMicrosoft.Webpages.Helpers.Toolkit.dll
下面我们就动手对ASP.NET MVC程序添加一个Razor的模板引擎:
首先建立一个ASP.NET MVC的项目,然后对其中的Web.Config的system.web/compilation/assemblies节点上添加内容:
<add assembly="Microsoft.WebPages.Compilation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add assembly="Microsoft.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add assembly="Microsoft.WebPages.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><add assembly="Microsoft.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
并对system.web/compilation/buildProviders添加内容:
<add extension=".cshtml" type="Microsoft.WebPages.Compilation.InlinePageBuildProvider" />
并引用相应的
Microsoft.Data.dll Microsoft.Webpages.dll Microsoft.Webpages.Helpers.dll Microsoft.Webpages.Compilation.dll
几个文件
准备工作做好了,下面就来实现相应的模板引擎,我们先来实现一个IView对象:
public class WebPageView : IView { public WebPageView(string viewPath) : this(viewPath, null) { } public WebPageView(string viewPath, string masterPath) { if (string.IsNullOrEmpty(viewPath)) { throw new ArgumentException("viewPath can't null", "viewPath"); } this.ViewPath = viewPath; this.MasterPath = masterPath ?? string.Empty; } public virtual void Render(ViewContext viewContext, TextWriter writer) { if (viewContext == null) { throw new ArgumentNullException("viewContext"); } WebPage page = WebPage.CreateInstanceFromVirtualPath(this.ViewPath);//load cshtml file if (page == null) { throw new InvalidOperationException("cshtml file not exists"); } else { this.RenderViewPage(viewContext, page); } } private void RenderViewPage(ViewContext context, WebPage page) { if (!string.IsNullOrEmpty(this.MasterPath)) { page.LayoutPage = this.MasterPath; } page.VirtualPath = this.ViewPath; page.ExecutePageHierarchy(CreatePageContext(context) , context.HttpContext.Response.Output, null); //execute cshtml file } internal static WebPageContext CreatePageContext(ViewContext content) { var pc = new WebPageContext(); var t = pc.GetType(); t.InvokeMember("HttpContext", BindingFlags.SetProperty | BindingFlags.NonPublic | BindingFlags.Instance , null, pc, new[] { content.HttpContext }); t.InvokeMember("ViewContext", BindingFlags.SetProperty | BindingFlags.NonPublic | BindingFlags.Instance , null, pc, new[] { content }); return pc; } ///Gets or sets the master path. ///The master path. public string MasterPath { get; private set; } ///Gets or sets the view path. ///The view path. public string ViewPath { get; private set; } }
然后我们再来实现一个IViewEngine对象:
////// WebPage View Engine /// class WebPageEngine : VirtualPathProviderViewEngine { public WebPageEngine() { // how to find the template path base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" }; base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" }; base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" }; base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" }; base.PartialViewLocationFormats = base.ViewLocationFormats; base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats; } protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { return new WebPageView(partialPath, null); } protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { return new WebPageView(viewPath, masterPath); } }
这样我们就实现了Razor的模板引擎了,我们只要在Global.asax中将模板引擎添加进去:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new WebPageEngine()); }
并且将Global的基类改为WebPageHttpApplication:
public class MvcApplication : WebPageHttpApplication{//...}
这样整个程序就可以工作了
我们在Views/Home下添加一个Index.cshtml:
@Html.ActionLink("Home","index","User")
@ViewData["Message"]
这样在我们访问/Home/Index的时候就可以得到ASP.NET MVC默认工程的HomeController.Index所生成的页面了:
Home
欢迎使用 ASP.NET MVC!
可见在这个模板引擎中,先天对ASP.NET MVC有良好的支持,本身已经集成了Helper、ViewData等诸多ASP.NET MVC的特性。
让我们期待ASP.NET MVC 3.0及Razor对VS的支持吧
8189E6B8-FBE4-4F01-8F9F-5687C0EA9F59
- 为ASP.NET MVC 2.0添加Razor模板引擎 (on .NET4)
- ASP.NET MVC Razor 视图引擎
- ASP.NET MVC Razor视图引擎攻略
- ASP.NET MVC Razor视图引擎
- ASP.NET MVC Razor视图引擎攻略
- @Razor 引擎 & Jquery in Asp.net MVC
- MVC Razor模板引擎
- MVC Razor模板引擎
- Asp.Net MVC Razor
- Asp.net MVC 3 之 Razor 视图引擎
- Asp.net MVC 3 之 Razor 视图引擎
- 逃脱Asp.Net MVC框架/枷锁,使用Razor视图引擎
- 逃脱Asp.Net MVC框架/枷锁,使用Razor视图引擎
- 逃脱Asp.Net MVC框架/枷锁,使用Razor视图引擎
- Asp.net MVC 3 之 Razor 视图引擎
- ASP.NET MVC的Razor引擎:View编译原理
- ASP.NET MVC 3 Razor 视图引擎 基本语法
- ASP.NET MVC学习---(七)Razor视图引擎语法
- LM/NTLM验证机制
- 悟透JS
- 感谢某人
- 怎样把自己培养成为一个优秀的程序员
- Makefile中的变量
- 为ASP.NET MVC 2.0添加Razor模板引擎 (on .NET4)
- tomcat6.0配置(含配置视频下载)
- ActionForm使用java.util.Date数据类型解决方法(转)
- 黑莓bold模拟器无法上网问题之解决
- C#基础系列(8)-- 第三部分 面向对象相关 -- 类与结构(1)
- printf实现动态显示
- ZOJ 1337 Pi
- ADDS 硬盘分区
- ExtJS实用开发指南