做qq空间动态皮肤网站,网页美工设计培训学校哪家好,自己设计好的网站怎么设置访问,深圳网站建设智能 乐云践新使用ASP.NET MVC时#xff0c;我们知道#xff0c;要使用Views中的视图#xff0c;需要在Action中写 return View(); 这个方法返回的返回值是一个 ViewResult#xff0c;进入这个类#xff0c;继承了父类ViewResultBase后只写了MasterName属性和FindView方法。 不过已经开…使用ASP.NET MVC时我们知道要使用Views中的视图需要在Action中写 return View(); 这个方法返回的返回值是一个 ViewResult进入这个类继承了父类ViewResultBase后只写了MasterName属性和FindView方法。 不过已经开始看到到ViewEngine的踪影了。 protected override ViewEngineResult FindView(ControllerContext context) {ViewEngineResult result } 跟进ViewEngineCollection.FindView去看看。 来到了ViewEngineCollection类这是一个实现了CollectionIViewEngine的类。 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(FuncIViewEngine, ViewEngineResult cacheLocator, FuncIViewEngine, 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(FuncIViewEngine, ViewEngineResult lookup, bool trackSearchedPaths) {// Returns// 1st result// OR list of searched paths (if trackSearchedPaths true)// OR nullViewEngineResult result;Liststring searched null;if (trackSearchedPaths) {searched new Liststring();}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