当前位置:首页 > 网络编程 > WEB编程 > ASP.net > ASP.NET MVC实现自己的视图引擎

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

点击次数:18 次 发布日期:2008-11-27 01:12:40 作者:源代码网
源代码网推荐

源代码网整理以下在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类,

源代码网整理以下 源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华