在上一篇文章中,我们知道了通过Controller执行ActionResult的Execute可以找到对应Controler对应的ViewEngine,然后在View中把Action的结果显示出来。那么ViewEngine到底是如何工作的?

我们首先从ViewReult的FindView方法开始

protectedoverrideViewEngineResult FindView(ControllerContext context)

{

ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, MasterName);

if (result.View != null)

{

return result;

}

// we need to generate an exception containing all the locations we searched

}

根据ControllerContext和ViewName,以及MasterName找到对应的ViewEngineResult对象。我们还是以HelloController和Index为例。那么这里result将返回Views/Hello/Index.cshtml编译后的实例。我们进入FindView方法,看看其具体的实现。

上述方法调用的是ViewEngineCollection类的虚方法FindView

publicvirtualViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName)

{

if (controllerContext == null)

{

thrownewArgumentNullException("controllerContext");

}

if (String.IsNullOrEmpty(viewName))

{

thrownewArgumentException(MvcResources.Common_NullOrEmpty, "viewName");

}

return Find(e => e.FindView(controllerContext, viewName, masterName, true),

e => e.FindView(controllerContext, viewName, masterName, false));

}

它又调用其私有的方法

privateViewEngineResult 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 returned

return Find(cacheLocator, trackSearchedPaths: false)

?? Find(locator, trackSearchedPaths: true);

}

然后又调用

privateViewEngineResult Find(Func<IViewEngine, ViewEngineResult> lookup, bool trackSearchedPaths)

{

// Returns

// 1st result

// OR list of searched paths (if trackSearchedPaths == true)

// OR null

ViewEngineResult result;

List<string> searched = null;

if (trackSearchedPaths)

{

searched = newList<string>();

}

foreach (IViewEngine engine in CombinedItems)

{

if (engine != null)

{

result = lookup(engine);

if (result.View != null)

{

return result;

}

if (trackSearchedPaths)

{

searched.AddRange(result.SearchedLocations);

}

}

}

if (trackSearchedPaths)

{

// Remove duplicate search paths since multiple view engines could have potentially looked at the same path

returnnewViewEngineResult(searched.Distinct().ToList());

}

else

{

returnnull;

}

}

注意,这里传入了一个Func<IViewEngine, ViewEngineResult>。输入一个IViewEngine类型,请注意ViewEngine是实际来自ViewEnglieCollection的属性CombinedItems。该对象来自IResolver<IEnumerable<IViewEngine>>接口的Current属性,其类型为IEnuerable<IViewEngine>。其实就是传入一个IViewEngine,然后一个ViewEngileResult。比如下面的例子:

// details of ViewEngineCollection.Find(*)

Func<IViewEngine, System.Web.Mvc.ViewEngineResult> cacheLocator = e => e.FindView(ControllerContext, "ViewInstance", "", false);

IViewEngine razorViewEngine = newRazorViewEngine();

System.Web.Mvc.ViewEngineResult result = cacheLocator(razorViewEngine);

请注意,IResolver<IEnumerable<IViewEngine>>的默认实例是newMultiServiceResolver<IViewEngine>(() => Items);
【有待于确认】。我们查看其构造函数,可以发现

public MultiServiceResolver(Func<IEnumerable<TService>> itemsThunk)

{

if (itemsThunk == null)

{

thrownewArgumentNullException("itemsThunk");

}

_itemsThunk = itemsThunk;

_resolverThunk = () => DependencyResolver.Current;

_itemsFromService = newLazy<IEnumerable<TService>>(() => _resolverThunk().GetServices<TService>());

}

也就是说IResolver的实例来自DependencyResolver.Current。通过DependencyResolver的定义,我们得知Current属性返回的单列new DenpendencyResolver.InnerCurrent,InnerCurrent其实就是newDefaultDependencyResolver()。DedendencyResolver在创建,创建单列的DefaultDependencyResolver,并将其赋值给_current和_currentCache。_currentCache对应的是InnerCurrentCache,该属性在创建View使没有使用,在创建Controller的时候会使用。

然后,调用ViewResult对象View属性的Render方法,次方法首先会创建View实例。

object instance = null;

Type type = BuildManager.GetCompiledType(ViewPath);

if (type != null)

{

instance = ViewPageActivator.Create(_controllerContext, type);

}

而实际上,现在的ViewPageActivator实际是newBuildManagerViewEngine.DefaultViewPageActivator(dependencyResolver),而这里的denpendencyResolver就是DefaultDependencyResolver。

OK,我们最后来看一下instance是如何创建的:

_resolverThunk().GetService(type) ?? Activator.CreateInstance(type);

请注意,_resolverThunk = () => DependencyResolver.Current;由此可见在默认的PageActivator内部的私有Fun变量_resolverThunk同样来自DefaultDependencyResolver.Current。

而instance要么DefaultDependencyResolver的GetService()创建,要么通过Activator.CreateInstance来创建,其实它们内部根本没有多少差别。

publicobject GetService(Type serviceType)

{

// Since attempting to create an instance of an interface or an abstract type results in an exception, immediately return null

// to improve performance and the debugging experience with first-chance exceptions enabled.

if (serviceType.IsInterface || serviceType.IsAbstract)

{

returnnull;

}

try

{

returnActivator.CreateInstance(serviceType);

}

catch

{

returnnull;

}

}

由此可见,GetService内部还是调用了Activator.CreateInstance方法创建实例。

剖析如何获取View的具体信息

  1. 获取View的Path

// show the details of retrieving view path

publicActionResult List()

{

string[] ViewLocationFormats = new[]

{

"~/Views/{1}/{0}.cshtml",

"~/Views/{1}/{0}.vbhtml",

"~/Views/Shared/{0}.cshtml",

"~/Views/Shared/{0}.vbhtml"

};

string name = "List";

string controllerName = "InnerView";

string areaName = "";

List<ViewLocation> allLocations = newList<ViewLocation>();

foreach (string viewLocationFormat in ViewLocationFormats)

allLocations.Add(newViewLocation(viewLocationFormat));

DisplayModeProvider instance = DisplayModeProvider.Instance;

for (int i = 0; i < allLocations.Count; i++)

{

ViewLocation location = allLocations[i];

string virtualPath = location.Format(name, controllerName, areaName);

DisplayInfo virtualPathDisplayInfo = instance.GetDisplayInfoForVirtualPath(virtualPath, ControllerContext.HttpContext,

path => FileExists(ControllerContext, path), null);

if (virtualPathDisplayInfo == null)

continue;

var viewPath = virtualPathDisplayInfo.FilePath;

Response.Write(viewPath);

}

return View();

}

返回结果为:

  1. 获取View的对象

publicActionResult Activator()

{

// remove the dependency

IDependencyResolver dependencyResolver = DependencyResolver.Current;

IResolver<IViewPageActivator> activatorResolver = newSingleServiceResolver<IViewPageActivator>(

() => null, newDefaultViewPageActivator(dependencyResolver), "BuildManagerViewEngine constructor");

IViewPageActivator pageActivator = activatorResolver.Current;

// HelloMVC.Controllers.InnerViewController

Object controllerInstance = pageActivator.Create(ControllerContext, this.GetType());

// Page instance

IBuildManager buildManager = newBuildManagerWrapper();

Object viewpageInstance = pageActivator.Create(ControllerContext, buildManager.GetCompiledType("~/Views/InnerView/List.cshtml "));

Response.Write(string.Format("controllerInstance is {0} <br /> viewpageInstance is {1}", controllerInstance.GetType(), viewpageInstance.GetType()));

return View();

}

http://www.professionals-helpdesk.com/2012/08/exploring-mvc-framwwork-in-deep_10.html exploring MVC framework in deep – DependencyResolver Class

[ASP.NET MVC]视图是如何呈现的 (续)的更多相关文章

  1. [ASP.NET MVC]视图是如何呈现的

    为了搞清楚ASP.NET MVC的请求过程,我们计划从结果追踪到源头.使用VS2012创建一个空白的ASP.NET MVC项目 然后创建一个HelloController 创建一个HelloView. ...

  2. ASP.NET MVC 视图(五)

    ASP.NET MVC 视图(五) 前言 上篇讲解了视图中的分段概念.和分部视图的使用,本篇将会对Razor的基础语法简洁的说明一下,前面的很多篇幅中都有涉及到视图的调用,其中用了很多视图辅助器,也就 ...

  3. ASP.NET MVC 视图(四)

    ASP.NET MVC 视图(四) 前言 上篇对于利用IoC框架对视图的实现进行依赖注入,最后还简单的介绍一下自定义的视图辅助器是怎么定义和使用的,对于Razor语法的细节和辅助器的使用下篇会说讲到, ...

  4. ASP.NET MVC 视图(一)

    ASP.NET MVC 视图(一) 前言 从本篇开始就进入到了MVC中的视图部分,在前面的一些篇幅中或多或少的对视图和视图中的一些对象的运用进行了描述,不过毕竟不是视图篇幅说的不全面,本篇首先为大家讲 ...

  5. Asp.net MVC 视图引擎

    Asp.net MVC视图引擎有两种: 1.ASPX View Engine 这个做过WebForm的人都清楚 设计目标:一个用于呈现Web Form页面的输出的视图引擎. 2.Razor View ...

  6. ASP.NET MVC 之Model的呈现

    ASP.NET MVC 之Model的呈现(仅此一文系列三) 本文目的 我们来看一个小例子,在一个ASP.NET MVC项目中创建一个控制器Home,只有一个Index: public class H ...

  7. 【ASP.NET MVC系列】浅谈ASP.NET MVC 视图

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  8. ASP.NET MVC 视图(三)

    ASP.NET MVC 视图(三) 前言 上篇对于Razor视图引擎和视图的类型做了大概的讲解,想必大家对视图的本身也有所了解,本篇将利用IoC框架对视图的实现进行依赖注入,在此过程过会让大家更了解的 ...

  9. ASP.NET MVC 视图(二)

    ASP.NET MVC 视图(二) 前言 上篇中对于视图引擎只是做了简单的演示,对于真正的理解视图引擎的工作过程可能还有点模糊,本篇将会对由MVC框架提供给我们的Razor视图引擎的整个执行过程做一个 ...

随机推荐

  1. 用户人品预测大赛--getmax队--竞赛分享

     用户人品预测大赛--getmax队--竞赛分享  DataCastle运营 发表于 2016-3-24 14:49:32      533  0  0 答辩PPT  

  2. js获取客户端time,cookie,url,ip,refer,user_agent信息:

    <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script type=& ...

  3. Android性能优化-内存优化

    原文链接 Manage Your App’s Memory 前言 在任何软件开发环境中,RAM都是比较珍贵的资源.在移动操作系统上更是这样,因为它们的物理内存通常受限.尽管在ART和Dalvik虚拟机 ...

  4. ios mac 对照片进行JPEG压缩

    ios mac 对照片进行JPEG压缩 1. 在iOS上可以使用 API UIImageJPEGRepresentation 对照片数据进行JPEG压缩: 我们知道iOS其实是MAC OS 的移植,那 ...

  5. Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  6. 用Docker启动php-5.6 fpm服务配合宿主机nginx运行php测试环境

    因为Ubuntu18.04默认的openssl版本就是1.1.0, 而PHP5.6无法在openssl 1.1下编译 "PHP 5.6 is receiving only security ...

  7. (原)Show, Attend and Translate: Unsupervised Image Translation with Self-Regularization and Attention

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9333844.html 论文网址:https://arxiv.org/abs/1806.06195 在 ...

  8. nodeJS服务器的创建和重新启动

    一: 首先在nodejs项目里创建一个server.js文件,输入下面代码 var http = require("http"); http.createServer(functi ...

  9. 基于jQuery虾米音乐播放器样式代码

    分享一款基于jQuery虾米音乐播放器样式代码.这是一款基于jquery+html5实现的虾米音乐播放器源码下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div c ...

  10. <时间的玫瑰>读书笔记

    投资不需要高等数学,只需要常识和智慧 一个人在市场里的输赢结果,实际上是对他人性优劣的奖惩 投资像孤独的乌龟与时间竞赛 时间是最有价值的资产,我们今天买入的股票不仅仅属于我们自己,它属于整个家族,我们 ...