コード例 #1
0
        private ModuleViewLocationCacheResult LocatePageFromPath(string executingFilePath, string pagePath)
        {
            var applicationRelativePath = GetAbsolutePath(executingFilePath, pagePath);
            var cacheKey = new ModuleViewLocationCacheKey(applicationRelativePath, null);

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

                var cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.SetSlidingExpiration(_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 ModuleViewLocationCacheResult(new[] { applicationRelativePath });
                }

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

            return(cacheResult);
        }
コード例 #2
0
        private ModuleViewLocationCacheResult OnCacheMiss(
            ViewLocationExpanderContext expanderContext,
            ModuleViewLocationCacheKey cacheKey)
        {
            IList <string> viewLocations = _options.ModuleViewLocationFormats;


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

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

                path = ModuleViewEnginePath.ResolvePath(path);

                cacheResult = CreateCacheResult(expirationTokens, path);
                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 ModuleViewLocationCacheResult(searchedLocations);
            }

            var cacheEntryOptions = new MemoryCacheEntryOptions();

            cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
            foreach (var expirationToken in expirationTokens)
            {
                cacheEntryOptions.AddExpirationToken(expirationToken);
            }

            return(ViewLookupCache.Set(cacheKey, cacheResult, cacheEntryOptions));
        }
コード例 #3
0
        private ViewEngineResult CreateViewEngineResult(ModuleViewLocationCacheResult result, string viewName)
        {
            if (!result.Success)
            {
                return(ViewEngineResult.NotFound(viewName, result.SearchedLocations));
            }

            var page = result.ViewEntry.PageFactory();

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

            for (var i = 0; i < viewStarts.Length; 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));
        }