[源码学习]调试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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/361437.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用Maven原型高效创建Eclipse模块

Maven Archetype是一个项目模板工具包&#xff0c;可为开发人员提供生成内置或自定义脚手架工件的参数化版本的方法。 最近&#xff0c;我将其应用于我们的Xiliary P2存储库&#xff0c;以实现Eclipse模块存根创建的自动化。 由于效果很好&#xff0c;所以我认为值得在这篇文章…

ubuntu 编译 /usr/bin/ld: cannot find 问题解决

参考文档&#xff1a; http://www.iq38.com/38536.html linux下编译应用程序常常会出现如下错误&#xff1a; /usr/bin/ld: cannot find -lxxx 意思是编译过程找不到对应库文件。其中&#xff0c;-lxxx表示链接库文件 libxxx.so。 注&#xff1a;有时候&#xff0c;由于库文件是…

CSS知识点整理(2):框模型,定位

1. 框模型&#xff1a;Box Model 规定了元素处理元素框处理元素内容、外边距、边框、内边距的方式。 2. 当边距给定的值 可以小于4个。CSS定义了一些规则、处理这中情况&#xff1a; 如果缺少左外边距的值&#xff0c;则使用右外边距的值。如果缺少下外边距的值&#xff0c;则…

Vux的安装使用

1、Vux的安装 1.1、vue-cli的vux模板生成项目 可以直接使用 vue-cli 的模板生成一个 vux 项目 vue init airyland/vux2 projectName 由此可以直接使用 vux。&#xff08;或许运行项目可能会报错&#xff0c;那是 vue-cli 初始项目的通病&#xff09; 1.2、手动安装 vux 首先在项…

Spring Data JPA教程:获取所需的依赖关系

在创建使用Spring Data JPA的应用程序之前&#xff0c;我们需要获取所需的依赖关系。 这篇博客文章标识了必需的组件&#xff0c;并描述了如何使用Maven获得它们。 让我们开始吧。 其他阅读&#xff1a;如果您不熟悉Spring Data JPA&#xff0c;则应该阅读以下博客文章&…

元素属性的添加删除(原生js)

添加属性 odiv.setAttribute("title","hello div!");odiv.setAttribute("class","boxClass");odiv.setAttribute("hello","divTag");//自定义属性设(hello"divTag") 获取属性 var vodiv.getAttribute(&…

framelayout

编写的mail.xml文件: <?xml version"1.0" encoding"utf-8"?><FrameLayout xmlns:android"http://schemas.android.com/apk/res/android" android:id"id/frame" android:layout_width"fill_parent" android:layou…

Git 使用的问题总结

1、git stash pop 显示 xxx already exists, no checkout 当我们先使用 git stash save -u 保存信息说明 来储藏更改&#xff0c;然后拉取代码 git pull&#xff0c;如果你的本地修改有新建文件&#xff0c;远程也有新建文件&#xff0c;并且两者同名&#xff0c;此时应用储藏 …

在OpenShift上托管的WildFly实例上进行Arquillian测试

技术提示&#xff03;54解释了如何为现有Java EE项目启用Arquillian。 在该技巧中&#xff0c;测试是针对本地安装的WildFly服务器运行的。 如果此WildFly实例在OpenShift上运行&#xff0c;那么同一个适配器也可以工作吗&#xff1f; 没有&#xff01; 因为与xlocalhost相比&…

js基础---数组方法

数组数据的排序及去重   sort无形参的排序方式 arr1[2,12,3,15];var aarr1.sort();console.log(arr1);console.log(a);//排序会改变原本数组是顺序&#xff0c;是依据首个字符的大小开始排名sort有形参的排序方式方法1arr2[2,12,3,15];var barr2.sort(function(n1,n2){if(n1&…

扩展Asterisk1.8.7的CLI接口

我之前有一篇文章&#xff08;http://www.cnblogs.com/MikeZhang/archive/2012/04/14/asteriskCLIAppTest20120414.html&#xff09;介绍过如何扩展asterisk的cli接口&#xff0c;本篇是它的继续&#xff0c;总结下&#xff0c;也方便我以后查阅。 大部分情况下&#xff0c;配置…

CSS中的 ',' 、''、'+'、'~'

1、群组选择器&#xff08;,&#xff09; /* 表示既h1&#xff0c;又h2 */ h1, h2 {color: red; } 2、后代选择器&#xff08;空格&#xff09; /* 表示 h1 下面的所有 span 元素&#xff0c;不管是否以 h1 为直接父元素 */ h1 span {} 3、子元素选择器&#xff08;>&#x…

避免不必要的Spring配置组件扫描

我在堆栈溢出中遇到了一个有趣的问题。 Brett Ryan有问题&#xff0c;Spring Security配置被初始化了两次。 当我查看他的代码时&#xff0c;我发现了问题所在。 让我展示显示代码。 他有相当标准的Spring应用程序&#xff08;不使用Spring Boot&#xff09;。 使用基于Spring…

正则表达式 小结

时间&#xff1a;2018年1月21日 18:29:01 用于&#xff1a;此小结是学习正则表达式的总结 正则表达式&#xff08;regular expression&#xff09;小结1. . 表示出断行外任意一个字符   ^行首   $行尾   [] 表示范围 如&#xff1a;[a-z]指a到z范围的一个字符 [\u]表…

js 之for..in、表单及事件触发

<html ><body> <script type"text/javascript">var x;var mycarsnew Array();mycars[0] "saa";mycars[1] "Va";mycars[2] "BMW";for(x in mycars)//x是变量用来指定变量&#xff0c;指定的变量可以是数组元素&…

移动端,fixed bottom问题

//不显示 .bar {position:fixed;bottom:0;z-index:99; }//显示 .bar{position:fixed;bottom:calc(90vh); /*当前屏幕高度百分比90%*/z-index:99; } 转载于:https://www.cnblogs.com/qq917937712/p/11475310.html

js 判断一个字符在字符串中出现的次数

<script type"text/javascript">var sdjh.doiwe.esd.d.ddd0sdd.d.; var n(s.split(.)).length-1; document.write(n);</script> 结果&#xff1a;6 更多专业前端知识&#xff0c;请上 【猿2048】www.mk2048.com

Spring Boot微服务的黑匣子测试是如此简单

当我需要进行原型设计&#xff0c;概念验证或在空闲时间使用一些新技术时&#xff0c;开始新项目对于Maven来说总是有点烦人。 不得不说&#xff0c;设置Maven项目并不难&#xff0c;您可以使用Maven原型。 但是原型通常是过时的。 谁想玩旧技术&#xff1f; 因此&#xff0c;我…

sharepoint ECMAScript对象模型系列

转载&#xff1a;Sharepoint学习笔记—ECMAScript对象模型系列-- 8、组与用户操作(一) http://www.cnblogs.com/wsdj-ITtech/archive/2012/06/08/2416967.html 转载于:https://www.cnblogs.com/EricLee007/archive/2012/06/08/2541636.html

c++ static关键字的作用

名称  全局静态变量  局部静态变量   静态函数  类的静态成员类的静态函数形式  全局变量前加static局部变量前加static 函数返回类型前加static类成员前加static类成员函数前加static存储区域 静态存储区 运行期间一直存在 静态存储区 运行期间一直存在 作用域…