protected virtual string GetPathFromGeneralName(ControllerContext controllerContext, List <ViewLocation> locations, string name, string controllerName, string areaName, string theme, string cacheKey, ref string[] searchedLocations)
        {
            string result = String.Empty;

            searchedLocations = new string[locations.Count];

            for (int i = 0; i < locations.Count; i++)
            {
                ViewLocation location               = locations[i];
                string       virtualPath            = location.Format(name, controllerName, areaName, theme);
                DisplayInfo  virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, controllerContext.HttpContext, path => FileExists(controllerContext, path), controllerContext.DisplayMode);

                if (virtualPathDisplayInfo != null)
                {
                    string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;

                    searchedLocations = _emptyLocations;
                    result            = resolvedVirtualPath;
                    ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, virtualPathDisplayInfo.DisplayMode.DisplayModeId), result);

                    if (controllerContext.DisplayMode == null)
                    {
                        controllerContext.DisplayMode = virtualPathDisplayInfo.DisplayMode;
                    }

                    // Populate the cache for all other display modes. We want to cache both file system hits and misses so that we can distinguish
                    // in future requests whether a file's status was evicted from the cache (null value) or if the file doesn't exist (empty string).
                    IEnumerable <IDisplayMode> allDisplayModes = DisplayModeProvider.Modes;
                    foreach (IDisplayMode displayMode in allDisplayModes)
                    {
                        if (displayMode.DisplayModeId != virtualPathDisplayInfo.DisplayMode.DisplayModeId)
                        {
                            DisplayInfo displayInfoToCache = displayMode.GetDisplayInfo(controllerContext.HttpContext, virtualPath, virtualPathExists: path => FileExists(controllerContext, path));

                            string cacheValue = String.Empty;
                            if (displayInfoToCache != null && displayInfoToCache.FilePath != null)
                            {
                                cacheValue = displayInfoToCache.FilePath;
                            }
                            ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), cacheValue);
                        }
                    }
                    break;
                }

                searchedLocations[i] = virtualPath;
            }

            return(result);
        }
示例#2
0
        protected virtual string GetPathFromSpecificName(ControllerContext controllerContext, string name,
                                                         string cacheKey, ref string[] searchedLocations)
        {
            var result = name;

            if (!(FilePathIsSupported(name) && FileExists(controllerContext, name)))
            {
                result            = String.Empty;
                searchedLocations = new[] { name };
            }

            ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result);
            return(result);
        }
        private string GetPathFromGeneralName(ControllerContext controllerContext, List <ViewLocation> locations, string name, string controllerName, string areaName, string cacheKey, ref string[] searchedLocations)
        {
            string result = String.Empty;

            searchedLocations = new string[locations.Count];

            for (int i = 0; i < locations.Count; i++)
            {
                ViewLocation location               = locations[i];
                string       virtualPath            = location.Format(name, controllerName, areaName);
                DisplayInfo  virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, controllerContext.HttpContext, path => FileExists(controllerContext, path), controllerContext.DisplayMode);

                if (virtualPathDisplayInfo != null)
                {
                    string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;

                    searchedLocations = _emptyLocations;
                    result            = resolvedVirtualPath;
                    ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, virtualPathDisplayInfo.DisplayMode.DisplayModeId), result);

                    if (controllerContext.DisplayMode == null)
                    {
                        controllerContext.DisplayMode = virtualPathDisplayInfo.DisplayMode;
                    }

                    // Populate the cache with the existing paths returned by all display modes.
                    // Since we currently don't keep track of cache misses, if we cache view.aspx on a request from a standard browser
                    // we don't want a cache hit for view.aspx from a mobile browser so we populate the cache with view.Mobile.aspx.
                    IEnumerable <IDisplayMode> allDisplayModes = DisplayModeProvider.Modes;
                    foreach (IDisplayMode displayMode in allDisplayModes)
                    {
                        if (displayMode.DisplayModeId != virtualPathDisplayInfo.DisplayMode.DisplayModeId)
                        {
                            DisplayInfo displayInfoToCache = displayMode.GetDisplayInfo(controllerContext.HttpContext, virtualPath, virtualPathExists: path => FileExists(controllerContext, path));

                            if (displayInfoToCache != null && displayInfoToCache.FilePath != null)
                            {
                                ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayInfoToCache.DisplayMode.DisplayModeId), displayInfoToCache.FilePath);
                            }
                        }
                    }
                    break;
                }

                searchedLocations[i] = virtualPath;
            }

            return(result);
        }
示例#4
0
        protected string GetPathFromSpecificName(
            ControllerContext controllerContext, string name, string cacheKey,
            ref string[] searchedLocations)
        {
            string virtualPath = name;

            if (!FileExists(controllerContext, name))
            {
                virtualPath       = string.Empty;
                searchedLocations = new string[] { name };
            }
            ViewLocationCache.InsertViewLocation(controllerContext.HttpContext,
                                                 cacheKey, virtualPath);
            return(virtualPath);
        }
        protected override string GetPathFromGeneralName(
            ControllerContext controllerContext,
            List <ViewLocation> locations,
            string name,
            string controllerName,
            string areaName,
            string theme,
            string cacheKey,
            ref string[] searchedLocations)
        {
            var parentThemes = LoadParentThemes(theme);

            string result           = string.Empty;
            var    checkedLocations = new List <string>();

            for (int i = 0; i < locations.Count; i++)
            {
                var    location               = locations[i];
                string virtualPath            = location.Format(name, controllerName, areaName, theme);
                var    virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(
                    virtualPath,
                    controllerContext.HttpContext,
                    path => FileExists(controllerContext, path),
                    controllerContext.DisplayMode);

                if (virtualPathDisplayInfo == null && virtualPath.IndexOf("/Themes/", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    // We're checking themes location. Try parent themes as well.

                    foreach (var parentTheme in parentThemes)
                    {
                        // 1. Track the last checked location
                        checkedLocations.Add(virtualPath);

                        // 2. Try parent theme
                        virtualPath            = location.Format(name, controllerName, areaName, parentTheme);
                        virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(
                            virtualPath,
                            controllerContext.HttpContext,
                            path => FileExists(controllerContext, path),
                            controllerContext.DisplayMode);

                        if (virtualPathDisplayInfo != null)
                        {
                            // 3. We found target view in the parent theme
                            break;
                        }
                    }
                }

                if (virtualPathDisplayInfo != null)
                {
                    string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;

                    checkedLocations.Clear();
                    result = resolvedVirtualPath;
                    ViewLocationCache.InsertViewLocation(
                        controllerContext.HttpContext,
                        AppendDisplayModeToCacheKey(cacheKey, virtualPathDisplayInfo.DisplayMode.DisplayModeId), result);

                    if (controllerContext.DisplayMode == null)
                    {
                        controllerContext.DisplayMode = virtualPathDisplayInfo.DisplayMode;
                    }

                    var allDisplayModes = DisplayModeProvider.Modes;
                    foreach (IDisplayMode displayMode in allDisplayModes)
                    {
                        if (displayMode.DisplayModeId != virtualPathDisplayInfo.DisplayMode.DisplayModeId)
                        {
                            var displayInfoToCache = displayMode.GetDisplayInfo(controllerContext.HttpContext, virtualPath, virtualPathExists: path => FileExists(controllerContext, path));

                            string cacheValue = string.Empty;
                            if (displayInfoToCache != null && displayInfoToCache.FilePath != null)
                            {
                                cacheValue = displayInfoToCache.FilePath;
                            }

                            ViewLocationCache.InsertViewLocation(
                                controllerContext.HttpContext,
                                AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), cacheValue);
                        }
                    }

                    break;
                }

                checkedLocations.Add(virtualPath);
            }

            searchedLocations = checkedLocations.ToArray();
            return(result);
        }
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName,
                                                  string masterName, bool useCache)
        {
            viewName = viewName.ToTitleCaseString();

            if (controllerContext == null)
            {
                throw new ArgumentNullException(nameof(controllerContext), "The controllerContext parameter is null");
            }

            if (string.IsNullOrEmpty(viewName))
            {
                throw new ArgumentException("The viewName parameter is null or empty.", nameof(viewName));
            }

            if (controllerContext.Controller == null)
            {
                return(base.FindView(controllerContext, viewName, masterName, useCache));
            }

            var pluginName = controllerContext.Controller.ToControllerShortName();

            if (string.IsNullOrEmpty(pluginName))
            {
                return(base.FindView(controllerContext, viewName, masterName, useCache));
            }
            var cacheKey = $"{pluginName}|{viewName}";

            if (useCache && ViewLocationCache != null)
            {
                var cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey);
                if (!string.IsNullOrEmpty(cachedLocation))
                {
                    return(new ViewEngineResult(CreateView(controllerContext, cachedLocation, masterName),
                                                this));
                }
            }

            string trimmedViewName;

            if (viewName.EndsWith(".cshtml") || viewName.EndsWith(".vbhtml"))
            {
                trimmedViewName = viewName.Remove(viewName.Length - 7);
            }
            else
            {
                trimmedViewName = viewName;
            }
            var args = new object[] { trimmedViewName, pluginName };

            foreach (var location in ViewLocationFormats)
            {
                var path = string.Format(location, args);

                if (!FileExists(controllerContext, path))
                {
                    continue;
                }

                ViewLocationCache?.InsertViewLocation(controllerContext.HttpContext, cacheKey, path);

                return(new ViewEngineResult(CreateView(controllerContext, path, masterName), this));
            }
            return(new ViewEngineResult(ViewLocationFormats.Select(i => string.Format(i, args))));
        }
        private ViewLocationResult FindView(IEnumerable<string> locations, string viewName, string fileNameFormat = "{0}", bool throwException = false)
        {
            var checkedLocations = new List<string>();
            var viewFound = false;
            var context = new HttpContextWrapper(HttpContext.Current);

            var viewNameWithExtension = string.Format(fileNameFormat, viewName);

            ViewLocationResult foundView = null;

            var themeDirectory = ThemeDirectory;
            var cacheKey = string.Format("{0}-{1}", themeDirectory, viewNameWithExtension);

            if (!string.IsNullOrEmpty(themeDirectory))
            {
                // check cache first
                foundView = ViewLocationCache.GetViewLocation(context, cacheKey) as ViewLocationResult;
                if (foundView != null) return foundView;

                foreach (var fullPath in locations.Select(viewLocation => Combine(this._baseDirectoryPath, themeDirectory, viewLocation, viewNameWithExtension)))
                {
                    var file = VirtualPathProviderHelper.GetFile(fullPath);
                    if (file != null)
                    {
                        foundView = new FileViewLocationResult(file, file.VirtualPath);
                        viewFound = true;
                        break;
                    }

                    checkedLocations.Add(fullPath);
                }

                // now search in global location
                // App_Data/Themes/_Global
                if (!viewFound)
                {
                    foreach (var fullPath in
                        locations.Select(
                            viewLocation => Combine(this._baseDirectoryPath, "_global", viewLocation, viewNameWithExtension)))
                    {
                        var file = VirtualPathProviderHelper.GetFile(fullPath);
                        if (file != null)
                        {
                            foundView = new FileViewLocationResult(file, file.VirtualPath);
                            viewFound = true;
                            break;
                        }

                        checkedLocations.Add(Path.Combine(this._baseDirectoryPath, fullPath));
                    }
                }
            }

            if (foundView == null /*!viewFound && throwException*/)
            {
                foundView = new ViewLocationResult(checkedLocations.ToArray());
            }

            ViewLocationCache.InsertViewLocation(context, cacheKey, foundView);

            return foundView;
        }