推薦:淺談ASP.NET中使用AJAX的簡單方法據我所知,這項技術最初是由Microsoft在1999年提出來的,也就是我們所熟知的使用遠程調用(remote calls)的DHTML / JavaScript web應用程序.這項技術的核心就是通過瀏覽器發出一個異步的HTTP請求來調用服務端的網頁或服務,在返回結果后,無需刷新就可以更
在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 } |
| 以下為引用的內容: 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[] { "" }; } } |
分享:解讀ASP.NET編寫應用程序的十大技巧1、在使用Visual Studio .NET時,除直接或非引用的對象外,不要使用缺省的名字。 .NET帶來的好處之一是所有的源代碼和配置文件都是純文本文件,能夠使用Notepad或WordPad等任意的文本編輯器進行編輯。如果不愿意,我們并非一定要使用Visual Studio .NET作為
新聞熱點
疑難解答
圖片精選