private PublicViewLocationCacheResult LocatePageFromPath(string executingFilePath, string pagePath, bool isMainPage)
        {
            var applicationRelativePath = GetAbsolutePath(executingFilePath, pagePath);
            var cacheKey = new PublicViewLocationCacheKey(applicationRelativePath, isMainPage);

            if (!ViewLookupCache.TryGetValue(cacheKey, out PublicViewLocationCacheResult cacheResult))
            {
                var expirationTokens = new HashSet <IChangeToken>();
                cacheResult = CreateCacheResult(expirationTokens, applicationRelativePath, isMainPage);

                var cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.SetSlidingExpiration(this.options.CacheExpirationDuration);
                foreach (var expirationToken in expirationTokens)
                {
                    cacheEntryOptions.AddExpirationToken(expirationToken);
                }

                // No views were found at the specified location. Create a not found result.
                if (cacheResult == null)
                {
                    cacheResult = new PublicViewLocationCacheResult(new[] { applicationRelativePath });
                }

                cacheResult = ViewLookupCache.Set(
                    cacheKey,
                    cacheResult,
                    cacheEntryOptions);
            }

            return(cacheResult);
        }
        private PublicViewLocationCacheResult OnCacheMiss(ViewLocationExpanderContext expanderContext, PublicViewLocationCacheKey cacheKey)
        {
            var viewLocations = GetViewLocationFormats(expanderContext);

            var expanders = options.ViewLocationExpanders;
            // Read interface .Count once rather than per iteration
            var expandersCount = expanders.Count;

            for (var i = 0; i < expandersCount; i++)
            {
                viewLocations = expanders[i].ExpandViewLocations(expanderContext, viewLocations);
            }

            PublicViewLocationCacheResult cacheResult = null;
            var searchedLocations = new List <string>();
            var expirationTokens  = new HashSet <IChangeToken>();

            foreach (var location in viewLocations)
            {
                var path = string.Format(
                    CultureInfo.InvariantCulture,
                    location,
                    expanderContext.ViewName,
                    expanderContext.ControllerName,
                    expanderContext.AreaName);

                path = PublicViewEnginePath.ResolvePath(path);

                cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage);
                if (cacheResult != null)
                {
                    break;
                }

                searchedLocations.Add(path);
            }

            // No views were found at the specified location. Create a not found result.
            if (cacheResult == null)
            {
                cacheResult = new PublicViewLocationCacheResult(searchedLocations);
            }

            var cacheEntryOptions = new MemoryCacheEntryOptions();

            cacheEntryOptions.SetSlidingExpiration(this.options.CacheExpirationDuration);
            foreach (var expirationToken in expirationTokens)
            {
                cacheEntryOptions.AddExpirationToken(expirationToken);
            }

            return(ViewLookupCache.Set(cacheKey, cacheResult, cacheEntryOptions));
        }
        internal virtual ViewEngineResult CreateViewEngineResult(PublicViewLocationCacheResult result, string viewName)
        {
            if (!result.Success)
            {
                return(ViewEngineResult.NotFound(viewName, result.SearchedLocations));
            }

            var page = result.ViewEntry.PageFactory();
            var cnt  = result.ViewStartEntries.Count;

            var viewStarts = new IRazorPage[result.ViewStartEntries.Count];

            for (var i = 0; i < cnt; i++)
            {
                var viewStartItem = result.ViewStartEntries[i];
                viewStarts[i] = viewStartItem.PageFactory();
            }

            var view = new RazorView(this, pageActivator, viewStarts, page, htmlEncoder, diagnosticListener);

            return(ViewEngineResult.Found(viewName, view));
        }