日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[源码学习]调试Razor从哪里开始

發布時間:2023/12/2 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [源码学习]调试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从哪里开始的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。