asp.net mvc源码分析-ActionResult篇 FindView

本文深入探讨 ASP.NET MVC 中的 ActionResult 类型 ViewResult 的 FindView 方法,详细解释如何查找视图,包括 ViewEngineCollection 的工作原理,查找视图的逻辑,以及 VirtualPathProviderViewEngine 类的角色。通过分析,揭示了如何优化查找过程,提高性能。
摘要由CSDN通过智能技术生成

接着上篇asp.net mvc源码分析-ActionResult篇 ViewResult 中有ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, MasterName)这么一句,它究竟是怎么找到View的了?首先放我们看看你ViewEngineCollection中的FindView方法吧,其实就一句

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

不过这句干的事情可不少啊,调用内部的一个Find方法,

    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 returned
            return Find(cacheLocator, trackSearchedPaths: false)
                ?? Find(locator, trackSearchedPaths: true);

        }

这里的cacheLocator=e.FindView(controllerContext, viewName, masterName, true),locator=e.FindView(controllerContext, viewName, masterName, false),它也是在调用一个内部的find方法,

   private ViewEngineResult 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 = new List<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
                return new ViewEngineResult(searched.Distinct().ToList());
            }
            else {
                return null;
            }
        }
其中 trackSea
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值