熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET MVC實現我們自己的視圖引擎[1]

2022-06-13   來源: .NET編程 

  在ASPNET MVC的一個開源項目MvcContrib中為我們提供了幾個視圖引擎例如NVelocity Brail NHaml XSLT那麼如果我們想在ASPNET MVC中實現我們自己的一個視圖引擎我們應該要怎麼做呢?

  我們知道呈現視圖是在Controller中通過傳遞視圖名和數據到RenderView()方法來實現的我們就從這裡下手我們查看一下ASPNET MVC的源代碼看看RenderView()這個方法是如何實現的

protected virtual void RenderView(string viewName string
masterName object viewData) {
           ViewContext viewContext = new ViewContext(
ControllerContext viewName masterName viewData TempData);
           ViewEngineRenderView(viewContext);
}//

  這是P的源碼P略有不同原理差不多從上面的代碼我們可以看到Controller中的RenderView()方法主要是將ControllerContext viewName masterName viewData TempData這一堆東西封裝成ViewContext然後把ViewContext傳遞給ViewEngineRenderView(viewContext)沒錯我們這裡要實現的就是ViewEngine的RenderView()方法

  ASPNET MVC為我們提供了一個默認的視圖引擎這個視圖引擎叫做WebFormsViewEngine 從名字就可以看出這個視圖引擎是使用ASPNET web forms來呈現的在這裡我們要實現的視圖引擎所使用的模板用HTML文件吧簡單的模板示例代碼如下

<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN
http://wwwworg/TR/xhtml/DTD/xhtmltransitionaldtd
<html xmlns=http://wwwworg//xhtml>
http://wwwworg//xhtml
<head>
    <title>自定義視圖引擎示例</title>
</head>
<body>
    <h>{$ViewDataTitle}</h
    <p>{$ViewDataMessage}</p>
    <p>The following fruit is part of a string array: {$ViewDataFruitStrings[]}</p>
    <p>The following fruit is part of an object array: {$ViewDataFruitObjects[]Name}</p>
    <p>Heres 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 = ViewLocatorGetViewLocation
                                 (viewContext viewContextViewName);
            if (stringIsNullOrEmpty(viewLocation))
            {
                throw new InvalidOperationException(stringFormat
                    (View {} could not be found                  viewContextViewName));
            }
            string viewPath = viewContextHttpContextRequestMapPath(viewLocation);
            string viewTemplate = FileReadAllText(viewPath);
            //以下為模板解析
            IRenderer renderer = new PrintRenderer();
            viewTemplate = rendererRender(viewTemplate viewContext);
            viewContextHttpContextResponseWrite(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
    }

[]  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15013.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.