[源码学习]调试Razor从哪里开始
使用ASP.NET MVC時,我們知道,要使用Views中的視圖,需要在Action中寫
?
return View();?
這個方法返回的返回值是一個 ViewResult,進入這個類,繼承了父類ViewResultBase后只寫了MasterName屬性和FindView方法。
不過已經開始看到到ViewEngine的蹤影了。
?
protected override ViewEngineResult FindView(ControllerContext context) {ViewEngineResult result = } ?跟進ViewEngineCollection.FindView去看看。
來到了ViewEngineCollection類,這是一個實現了Collection<IViewEngine>的類。
?
public virtual ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName) {if (controllerContext == null) {throw new ArgumentNullException("controllerContext");}if (string.IsNullOrEmpty(viewName)) {throw new ArgumentException(MvcResources.Common_NullOrEmpty, "viewName");}return Find(e => e.FindView(controllerContext, viewName, masterName, true),e => e.FindView(controllerContext, viewName, masterName, false));} ? 很簡單的一個方法,跟進去 ? private ViewEngineResult Find(Func<IViewEngine, ViewEngineResult> cacheLocator, Func<IViewEngine, ViewEngineResult> locator) {// First, look up using the cacheLocator and do not track the searched paths in non-matching view engines// Then, look up using the normal locator and track the searched paths so that an error view engine can be returnedreturn Find(cacheLocator, trackSearchedPaths: false)?? Find(locator, trackSearchedPaths: true);}private ViewEngineResult Find(Func<IViewEngine, ViewEngineResult> lookup, bool trackSearchedPaths) {// Returns// 1st result// OR list of searched paths (if trackSearchedPaths == true)// OR nullViewEngineResult result;List<string> searched = null;if (trackSearchedPaths) {searched = new List<string>();}foreach (IViewEngine engine in CombinedItems) {if (engine != null) {if (trackSearchedPaths) {searched.AddRange(result.SearchedLocations);}}}if (trackSearchedPaths) {// Remove duplicate search paths since multiple view engines could have potentially looked at the same pathreturn new ViewEngineResult(searched.Distinct().ToList());}else {return null;}} ?乍一看,又是for又是if、else的有點不知所措,其實仔細一看結合上面的Find參數就能找到黃色加亮的幾句代碼關鍵代碼。
是遍歷了注冊ViewEngine集合,調用ViewEngine各自的FindView,誰能找到View,就用誰。
那么遍歷的ViewEngine集合怎么來的呢?要回到ViewResult的父類ViewResultBase中去看。
?
public ViewEngineCollection ViewEngineCollection {get {return _viewEngineCollection ?? ViewEngines.Engines;}set {_viewEngineCollection = value;}}?
如果沒有定義那么就調用ViewEngines.Engines,這是一個很簡單的靜態類屬性
?
public static class ViewEngines {private readonly static ViewEngineCollection _engines = new ViewEngineCollection {new WebFormViewEngine(),new RazorViewEngine(),};public static ViewEngineCollection Engines {get {return _engines;}}}?
回到剛才的遍歷,由于RazorViewEngine的構造函數中定義了以下格式,在Views文件夾中也創建了相應的文件,所以選擇了RazorViewEngine。
?
AreaViewLocationFormats = new[] {"~/Areas/{2}/Views/{1}/{0}.cshtml","~/Areas/{2}/Views/{1}/{0}.vbhtml","~/Areas/{2}/Views/Shared/{0}.cshtml","~/Areas/{2}/Views/Shared/{0}.vbhtml"}; ? 好了,終于找到了Razor。轉載于:https://www.cnblogs.com/llcto/archive/2012/06/02/2531470.html
總結
以上是生活随笔為你收集整理的[源码学习]调试Razor从哪里开始的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第四种行转列
- 下一篇: ubuntu 编译 /usr/bin/l