【赛迪网-it技术报道】在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>
< ml>
|
下面马上开始我们的实现。首先,毫无疑问的,我们要创建一个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[] { "~ iews/{1}/{0}.htm",
"~ iews/{1}/{0}.html",
"~ iews d/{0}.htm",
"~ iews d/{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中的四个视图引擎的代码。最近要对模板引擎进行研究,大家有什么其他优秀的、成熟的、开源的模板引擎,麻烦给小弟推荐一下,先谢了。