好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>高手进阶
<tfoot draggable='sEl'></tfoot>

ASP.NET MVC : 实现我们自己的视图引擎

ASP.NET MVC : 实现我们自己的视图引擎

本文短址: http://s8.hk/2e0

在ASP.NET MVC的一个开源项目 MvcContrib 中,为我们提供了几个视图引擎,例如NVelocity, Brail, NHaml, XSLT。那么如果我们想在ASP.NET MVC中实现我们自己的一个视图引擎,我们应该要怎么做呢?

我们知道呈现视图是在Controller中通过传递视图名和数据到RenderView()方法来实现的。好,我们就从这里下手。我们查看一下ASP.NET MVC的源代码,看看RenderView()这个方法是如何实现的:

 protected   virtual   void  RenderView( string  viewName,  string  masterName,  object  viewData) {
ViewContext viewContext = new ViewContext(ControllerContext, viewName, masterName, viewData, TempData);
ViewEngine.RenderView(viewContext);
} //这是P2的源码,P3略有不同,原理差不多

从上面的代码我们可以看到,Controller中的RenderView()方法主要是将ControllerContext, viewName, masterName, viewData, TempData这一堆东西封装成ViewContext,然后把ViewContext传递给 ViewEngine.RenderView(viewContext)。嗯,没错,我们这里要实现的就是ViewEngine的 RenderView()方法。

ASP.NET MVC为我们提供了一个默认的视图引擎,这个视图引擎叫做: WebFormsViewEngine. 从名字就可以看出,这个视图引擎是使用ASP.NET web forms来呈现的。在这里,我们要实现的视图引擎所使用的模板用HTML文件吧,简单的模板示例代码如下:

 < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html xmlns = "" http :// www . w3 . org / 1999 / xhtml "" > http://www.w3.org/1999/xhtml" >
< head >
< title > 自定义视图引擎示例 </ title >
</ head >
< body >
< h1 > {$ViewData.Title} </ h1 >
< p > {$ViewData.Message} </ p >
< p > The following fruit is part of a string array: {$ViewData.FruitStrings[1]} </ p >
< p > The following fruit is part of an object array: {$ViewData.FruitObjects[1].Name} </ p >
< p > Here's an undefined variable: {$UNDEFINED} </ p >
</ body >
</ html >

下面马上开始我们的实现。首先,毫无疑问的,我们要创建一个ViewEngine,就命名为 SimpleViewEngine 吧,注意哦,ViewEngine要实现 IViewEngine 接口:

 public   class  SimpleViewEngine : IViewEngine
{
# region Private members

IViewLocator _viewLocator = null ;

# endregion

# region IViewEngine Members : RenderView()

public void RenderView(ViewContext viewContext)
{
string viewLocation = ViewLocator.GetViewLocation(viewContext, viewContext.ViewName);
if ( string .IsNullOrEmpty(viewLocation))
{
throw new InvalidOperationException( string .Format(" View {0} could not be found. ", viewContext.ViewName));
}

string viewPath = viewContext.HttpContext.Request.MapPath(viewLocation);
string viewTemplate = File.ReadAllText(viewPath);

//以下为模板解析
IRenderer renderer = new PrintRenderer();
viewTemplate = renderer.Render(viewTemplate, viewContext);

viewContext.HttpContext.Response.Write(viewTemplate);
}

# endregion

# region Public properties : ViewLocator

public IViewLocator ViewLocator
{
get
{
if ( this ._viewLocator == null )
{
this ._viewLocator = new SimpleViewLocator();
}
return this ._viewLocator;
}
set
{
this ._viewLocator = value ;
}
}

# endregion
}

在这里实现了IViewEngine接口提供的RenderView()方法,这里要提供一个ViewLocator的属性。 ViewLocator的主要就是根据控制器中传来的视图名,进行视图的定位。在RenderView()方法中首先获取视图的路径,然后把视图模板读进 来,最后进行模板的解析然后输出。

我们再来看一下ViewLocator是如何实现的。他是IViewLocator类型的,也就是说SimpleViewLocator实现了IViewLocator接口。SimpleViewLocator的实现代码如下:

 public   class  SimpleViewLocator : ViewLocator
{
public SimpleViewLocator()
{
base .ViewLocationFormats = new string [] { " ~/Views/{1}/{0}.htm ",
" ~/Views/{1}/{0}.html ",
" ~/Views/Shared/{0}.htm ",
" ~/Views/Shared/{0}.html "
};
base .MasterLocationFormats = new string [] { " " };
}
}

我们的SimpleViewLocator 是继承自ASP.NET MVC的ViewLocator类,而ViewLocator则是实现了IViewLocator接口的。由于ViewLocator已经为了完成了全部的工作,这里我们只需修改下他的 ViewLocationFormats 来使用我们自己的模板文件就可以了。

我们再来看一下类图,那就更加清楚了:

注: 关于模板解析的部分代码这里就不说了,不在讨论范围内,可以自己下载代码来看 。

现在我们基本完成了我们的视图引擎,那么如何让ASP.NET MVC不要使用默认的web forms视图引擎,而使用我们自定义的视图引擎呢?

在ASP.NET MVC中,所有的请求都是通过一个工厂类来创建 Controller 实例的,这个工厂类必须实现 IControllerFactory 接口。默认的实现该接口的工厂类是DefaultControllerFactory。这个工厂类就是我们修改默认的视图引擎为我们的视图引擎的入口点。 为了方便,我们创建一个继承自DefaultControllerFactory的SimpleControllerFactory :

 public   class  SimpleControllerFactory : DefaultControllerFactory
{
protected override IController CreateController(RequestContext requestContext, string controllerName)
{
Controller controller = (Controller) base .CreateController(requestContext, controllerName);
controller.ViewEngine = new SimpleViewEngine(); //修改默认的视图引擎为我们刚才创建的视图引擎
return controller;
}
}

这里只要修改controller.ViewEngine为我们自定义的ViewEngine就可以了.最终的类图大概如下:

要使我们创建的控制器工厂类SimpleControllerFactory 成为默认的控制器工厂类,我们必须在Global.asax.cs中的Application_Start 事件中添加如下代码:

 ControllerBuilder .Current.SetControllerFactory( typeof (SimpleControllerFactory));

到这里,我们已经完成了我们自己的视图引擎。

在ASP.NET MVC中实现自定义的视图引擎是很简单的,难点在于模板的解析,具体大家可以研究 MvcContrib 中的四个视图引擎的代码。最近要对模板引擎进行研究,大家有什么其他优秀的、成熟的、开源的模板引擎,麻烦给小弟推荐一下,先谢了。

Enjoy!

版权声明:本文首发于 博客园 ,作者为 QLeelulu
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。

参考文章:
ASP.NET MVC Preview生命周期分析
Creating a custom ViewEngine for the ASP.NET MVC framework (下面给出的源代码就是这篇文章给出的源代码)

本文源码下载: ASP.NET MVC 自定义视图引擎源代码下载

?

NVelocity View Engine with Asp.net Mvc

因为要将CHSNS#转到Asp.net MVC平台上来,所以就有了在Asp.net Mvc平台下NVelocity的引擎问题

MvcContrib虽然也有NVelocity的引擎,不过整体错误不少.还必需要Castle.Windsor来动态引入ViewEngine

怎么看都不爽,于是自写了一个NVelocityEngine.

下载及源码: CHSNS NVelocity View Engine

使用方法为:

public   void  Application_Start( object  sender, EventArgs e) {
         ControllerBuilder.Current.SetControllerFactory( typeof (NVelocityEngine.NVelocityControllerFactory));
}

这样就可以直接使用它了

实现方法和lulu的文章是基本一致的: ASP.NET MVC : 实现我们自己的视图引擎 .

相关类及接口:

IViewDataContainer IViewEngine IViewLocator WebFormViewEngine ViewLocator WebFormViewLocator

Tag:NVelocity View Engine with Asp.net Mvc

Autumn Park is QBlog the official site, created by the passing autumn, based on the framework developed cyqdata data layer supports multiple users, multiple languages, multiple databases(access,mssql,oracle), directory level url and other powerful blog system

http://kb.cnblogs.com/page/88326/

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于ASP.NET MVC : 实现我们自己的视图引擎的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did50682

更新时间:2022-09-24   阅读:55次

上一篇: 新版微软一站式示例代码库发布 绑定第三版示例代码浏览器

下一篇:NVelocity用法 Net版

相关资讯

最新资料更新

  • 1.简单三步走搞定动态规划难题,记好这三板斧,动态规划就不难
  • 2.周而复始,往复循环,递归、尾递归算法与无限极层级结构的探究和使用(Golang1.18)
  • 3.机器学习随笔(决策树,集成算法,交叉验证)
  • 4.随机森林算法深入浅出
  • 5.算法题——给定一个数组 arr,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序
  • 6.动态开点线段树说明
  • 7.算法学习笔记(17): 快速傅里叶变换(FFT)
  • 8.推荐系统[八]算法实践总结V0:腾讯音乐全民K歌推荐系统架构及粗排设计
  • 9.算法提高 矩阵乘法
  • 10.数据结构和算法笔记
  • 11.《深入理解Java虚拟机》第三章读书笔记(一)——垃圾回收算法
  • 12.《算法导论》笔记 第8章 8.1排序算法时间的下界
  • 13.Label Propagation算法原理示例解析
  • 14.dfs与dp算法之关系与经典入门例题
  • 15.1164: 零起点学算法71——C语言合法标识符(存在问题)
  • 16.代码随想录算法训练营第二十九天| 491.递增子序列 46.全排列 47.全排列 II
  • 17.java算法题解LeetCode30包含min函数的栈实例
  • 18.1131: 零起点学算法38——求阶乘和
  • 19.联邦GNN综述与经典算法介绍
  • 20.数学建模算法-神经网络

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]