private string GetPathFromGeneralName(ControllerArgs controllerContext, ViewType viewType, string name) { var virtualPath = string.Empty; var locations = ViewLocations.Where(x => x.ViewType == viewType).ToList(); if ((locations == null) || (locations.Count == 0)) { throw new InvalidOperationException("locations must not be null or emtpy."); } List <string> sl = new List <string>(locations.Count); foreach (var loc in locations) { virtualPath = loc.GetPath(controllerContext, name); if (FileExists(controllerContext, virtualPath)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, controllerContext.GetChacheKey(viewType, name), virtualPath); return(virtualPath); } sl.Add(virtualPath); } var str = string.Join("\n", sl); throw new DirectoryNotFoundException(str); }
private string GetPathFromGeneralName(ControllerContext controllerContext, IEnumerable <ViewLocation> locations, IDisplayMode displayMode, string name, string controllerName, string areaName, string cacheKey, out IList <string> searchedLocations) { searchedLocations = new List <string>(); // Iterate through the locations. foreach (var location in locations) { var virtualPath = location.Format(name, controllerName, areaName); var virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, controllerContext.HttpContext, path => FileExists(controllerContext, path), controllerContext.DisplayMode); if (virtualPathDisplayInfo != null) { // Found a path for a view for a display mode. Cache it against the most specific display mode. var resolvedVirtualPath = virtualPathDisplayInfo.FilePath; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), resolvedVirtualPath); if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = virtualPathDisplayInfo.DisplayMode; } searchedLocations.Clear(); return(resolvedVirtualPath); } searchedLocations.Add(virtualPath); } // Even though it does not exist add it to the cache so that all subsequent lookups don't have to check again. ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), DoesNotExist); return(string.Empty); }
protected string GetPath(ControllerContext controllerContext, string[] locations, string locationsPropertyName, string name, string controllerName, string areaName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = EmptyLocations; if (string.IsNullOrEmpty(name)) { return(string.Empty); } if ((locations == null) || (locations.Length == 0)) { throw new InvalidOperationException(string.Format("The property " + "'{0}' cannot be null or empty.", locationsPropertyName)); } bool isSpecificPath = IsSpecificPath(name); string key = CreateCacheKey(cacheKeyPrefix, name, isSpecificPath ? string.Empty : controllerName, isSpecificPath ? string.Empty : areaName); if (useCache) { string viewLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key); if (viewLocation != null) { return(viewLocation); } } if (!isSpecificPath) { return(GetPathFromGeneralName(controllerContext, locations, name, controllerName, areaName, key, ref searchedLocations)); } return(GetPathFromSpecificName(controllerContext, name, key, ref searchedLocations)); }
protected string GetPathFromGeneralName(ControllerContext controllerContext, string[] locations, string name, string controllerName, string areaName, string cacheKey, ref string[] searchedLocations) { string virtualPath = string.Empty; searchedLocations = new string[locations.Length]; for (int i = 0; i < locations.Length; i++) { if (string.IsNullOrEmpty(areaName) && locations[i].Contains("{2}")) { continue; } string testPath = string.Format(CultureInfo.InvariantCulture, locations[i], name, controllerName, areaName); if (FileExists(controllerContext, testPath)) { searchedLocations = EmptyLocations; virtualPath = testPath; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath); return(virtualPath); } searchedLocations[i] = testPath; } return(virtualPath); }
protected virtual string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string themeName, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = EmptyLocations; if (string.IsNullOrEmpty(name)) { return(string.Empty); } string areaName = GetAreaName(controllerContext.RouteData); List <ViewLocation> viewLocations = GetViewLocations(locations, !string.IsNullOrEmpty(areaName) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The property '{0}' cannot be null or empty.", new object[] { locationsPropertyName })); } bool flag = IsSpecificPath(name); string key = CreateCacheKey(cacheKeyPrefix, name, flag ? string.Empty : controllerName, areaName, themeName); if (useCache) { var viewLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key); if (viewLocation != null) { return(viewLocation); } } return(!flag?GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, themeName, key, ref searchedLocations) : GetPathFromSpecificName(controllerContext, name, key, ref searchedLocations)); }
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); if (FileExists(controllerContext, virtualPath)) { searchedLocations = EmptyLocations; result = virtualPath; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result); break; } searchedLocations[i] = virtualPath; } return(result); }
private string GetPathFromSpecificName(ControllerContext controllerContext, string name, string rootFolder, string cacheKey, ref string[] searchedLocations) { var correctedName = name; if (correctedName.StartsWith("~")) { correctedName = correctedName.TrimStart('~'); } if (!correctedName.StartsWith("/")) { correctedName = "/" + correctedName; } var virtualPath = string.Concat(rootFolder, correctedName); var result = string.Empty; searchedLocations = new string[1]; if (!(FilePathIsSupported(virtualPath) && FileExists(controllerContext, virtualPath))) { result = string.Empty; searchedLocations[0] = virtualPath; } else { result = virtualPath; searchedLocations = _emptyLocations; } ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result); return(result); }
private string GetPath(ControllerContext controllerContext, IEnumerable <string> locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } var areaName = controllerContext.RouteData.GetAreaName(); var usingAreas = !String.IsNullOrEmpty(areaName); var viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, "{0} cannot be null or empty.", locationsPropertyName)); } var nameRepresentsPath = IsSpecificPath(name); var cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName); if (useCache) { return(ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey)); } return((nameRepresentsPath) ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } string areaName = AreaHelpers.GetAreaName(controllerContext.RouteData); bool usingAreas = !String.IsNullOrEmpty(areaName); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, MvcResources.Common_PropertyCannotBeNullOrEmpty, locationsPropertyName)); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName); if (useCache) { return(ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey)); } return((nameRepresentsPath) ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, string[] locations, string locationsPropertyName, string name, string themeNameOrAreaName, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (string.IsNullOrEmpty(name)) { return(string.Empty); } if ((locations == null) || (locations.Length == 0)) { throw new InvalidOperationException("locations must not be null or emtpy."); } bool flag = IsSpecificPath(name); string key = CreateCacheKey(cacheKeyPrefix, name, flag ? string.Empty : controllerName, themeNameOrAreaName); if (useCache) { var viewLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key); if (viewLocation != null) { return(viewLocation); } } return(!flag?GetPathFromGeneralName(controllerContext, locations, name, controllerName, themeNameOrAreaName, key, ref searchedLocations) : GetPathFromSpecificName(controllerContext, name, key, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, IEnumerable <string> locations, IEnumerable <string> areaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, bool checkPathValidity, ref bool incompleteMatch, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (string.IsNullOrEmpty(name)) { return(string.Empty); } string areaName = GetAreaName(controllerContext.RouteData); bool usingAreas = !string.IsNullOrEmpty(areaName); string theme = CurrentTheme(controllerContext.HttpContext); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The property '{0}' cannot be null or empty.", locationsPropertyName)); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = CreateCacheKey(theme, cacheKeyPrefix, name, (nameRepresentsPath) ? string.Empty : controllerName, areaName); if (useCache) { return(ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey)); } return(nameRepresentsPath ? GetPathFromSpecificName(controllerContext, name, cacheKey, checkPathValidity, ref searchedLocations, ref incompleteMatch) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, theme, cacheKey, ref searchedLocations)); }
private string GetPathFromSpecificName(ControllerContext controllerContext, string name, string cacheKey, bool checkPathValidity, ref string[] searchedLocations, ref bool incompleteMatch) { string result = name; bool fileExists = FileExists(controllerContext, name); if (checkPathValidity && fileExists) { bool?validPath = IsValidPath(controllerContext, name); if (validPath == false) { fileExists = false; } else if (validPath == null) { incompleteMatch = true; } } if (!fileExists) { result = string.Empty; searchedLocations = new[] { name }; } if (!incompleteMatch) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result); } return(result); }
private string GetPathFromGeneralName(ControllerContext controllerContext, List <ViewLocation> locations, string name, string controllerName, string areaName, string appName, string themeName, string cacheKey, ref List <string> searchedLocations) { string result = string.Empty; searchedLocations = new List <string>(); List <string> appNames = MrCMSApp.AppNames.OrderBy(s => s == appName ? 0 : 1).ToList(); foreach (ViewLocation location in locations) { foreach (string app in appNames) { string virtualPath = location.Format(name, controllerName, areaName, app, themeName); if (FileExists(virtualPath)) { searchedLocations = _emptyLocations; result = virtualPath; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result); return(result); } if (!searchedLocations.Contains(virtualPath)) { searchedLocations.Add(virtualPath); } } } return(result); }
protected virtual string GetPathFromGeneralName( ControllerContext controllerContext, List <ViewLocation> locations, string name, string controllerName, string areaName, string theme, string cacheKey, ref string[] searchedLocations) { var virtualPath = string.Empty; searchedLocations = new string[locations.Count]; for (var i = 0; i < locations.Count; i++) { var str2 = locations[i].Format(name, controllerName, areaName, theme); if (FileExists(controllerContext, str2)) { searchedLocations = _emptyLocations; virtualPath = str2; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath); return(virtualPath); } searchedLocations[i] = str2; } return(virtualPath); }
private string GetPathFromGeneralName(ControllerContext controllerContext, IList <ViewLocation> locations, string name, string controllerName, string areaName, string cacheKey, ref string[] searchedLocations) { var result = String.Empty; searchedLocations = new string[locations.Count]; string pluginFolder = string.Empty; if (controllerContext.HttpContext.Items.Contains(HttpContextItemKeys.PluginFolder)) { pluginFolder = (string)controllerContext.HttpContext.Items[HttpContextItemKeys.PluginFolder]; } for (var i = 0; i < locations.Count; i++) { var location = locations[i]; var virtualPath = ""; virtualPath = location.Format(name, controllerName, areaName, Theme, pluginFolder); if (FileExists(controllerContext, virtualPath)) { searchedLocations = _emptyLocations; result = virtualPath; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result); break; } searchedLocations[i] = virtualPath; } return(result); }
private string GetFromCache(ControllerContext controllerContext, string cacheKey) { using (MiniProfiler.Current.Step("Get from cache: " + cacheKey)) { return(ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey)); } }
private ViewEngineResult CacheLocation(ControllerContext controllerContext, string keyPath, string currentPath, string masterName, bool partial) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, currentPath); return(new ViewEngineResult(partial ? CreatePartialView(controllerContext, currentPath): CreateView(controllerContext, currentPath, masterName), this)); }
private ViewPathResult ResolveUsingCache( bool useCache, ControllerContext context, string viewName, Func <ViewPathResult> resolve) { if (!useCache) { return(resolve()); } var cacheKey = CreateCacheKey(context, viewName); var cachedLocation = ViewLocationCache.GetViewLocation(context.HttpContext, cacheKey); if (cachedLocation != null) { return(ViewPathResult.Create(cachedLocation)); } var resolved = resolve(); ViewLocationCache.InsertViewLocation(context.HttpContext, cacheKey, resolved.Path); return(resolved); }
private Tuple <string, string[]> ResolveViewPath( ControllerContext controllerContext, string viewName, IEnumerable <string> formats, bool useCache) { var featurePath = GetFeaturePath(controllerContext); var controllerName = GetControllerName(controllerContext); var cacheKey = CreateCacheKey(featurePath, viewName, controllerName); if (useCache) { var cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey); if (cachedLocation != null) { return(Tuple.Create(cachedLocation, new string[0])); } } var resolved = ResolveViewPath(controllerContext, viewName, formats); if (!string.IsNullOrEmpty(resolved.Item1)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, resolved.Item1); } return(Tuple.Create(resolved.Item1, resolved.Item2)); }
private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string themeName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } object appName; bool usingAreas = controllerContext.RouteData.Values.TryGetValue("appName", out appName); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "didn't find view {0} property", locationsPropertyName)); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : themeName, themeName); if (useCache) { return(ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey)); } return((nameRepresentsPath) ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, themeName, (appName ?? "").ToString(), cacheKey, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, string[] locations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } if (locations == null || locations.Length == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, MvcResources.Common_PropertyCannotBeNullOrEmpty, locationsPropertyName)); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName); if (useCache) { string result = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey); if (result != null) { return(result); } } return((nameRepresentsPath) ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, locations, name, controllerName, cacheKey, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } string areaName = AreaHelpers.GetAreaName(controllerContext.RouteData); bool usingAreas = !String.IsNullOrEmpty(areaName); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, MvcResources.Common_PropertyCannotBeNullOrEmpty, locationsPropertyName)); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName); if (useCache) { // Only look at cached display modes that can handle the context. IEnumerable <IDisplayMode> possibleDisplayModes = DisplayModeProvider.GetAvailableDisplayModesForContext(controllerContext.HttpContext, controllerContext.DisplayMode); foreach (IDisplayMode displayMode in possibleDisplayModes) { string cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId)); if (cachedLocation == null) { // If any matching display mode location is not in the cache, fall back to the uncached behavior, which will repopulate all of our caches. return(null); } // A non-empty cachedLocation indicates that we have a matching file on disk. Return that result. if (cachedLocation.Length > 0) { if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = displayMode; } return(cachedLocation); } // An empty cachedLocation value indicates that we don't have a matching file on disk. Keep going down the list of possible display modes. } // GetPath is called again without using the cache. return(null); } else { return(nameRepresentsPath ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations)); } }
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 index = 0; index < locations.Count; index++) { var location = locations[index]; string virtualPath = location.Format(name, controllerName, areaName, theme); var 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 (var 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?.FilePath != null) { cacheValue = displayInfoToCache.FilePath; } ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), cacheValue); } } break; } searchedLocations[index] = virtualPath; } return(result); }
private string GetPathFromGeneralName(ControllerContext controllerContext, List <ViewLocation> locations, string name, string controllerName, string areaName, string rootFolder, string cacheKey, ref string[] searchedLocations) { var result = string.Empty; searchedLocations = new string[locations.Count]; for (var i = 0; i < locations.Count; i++) { var location = locations[i]; var virtualPath = location.Format(name, controllerName, areaName, rootFolder); var virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, controllerContext.HttpContext, path => FileExists(controllerContext, path), controllerContext.DisplayMode); if (virtualPathDisplayInfo != null) { var 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 (var displayMode in allDisplayModes) { if (displayMode.DisplayModeId != virtualPathDisplayInfo.DisplayMode.DisplayModeId) { var displayInfoToCache = displayMode.GetDisplayInfo(controllerContext.HttpContext, virtualPath, 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); }
private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string[] moduleLocations, string[] moduleAreaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = g_emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } string moduleName = AreaHelpers.GetModuleName(controllerContext.RouteData); string areaName = AreaHelpers.GetAreaName(controllerContext.RouteData); bool usingModules = !string.IsNullOrEmpty(moduleName); bool usingAreas = !String.IsNullOrEmpty(areaName); List <ViewLocation> viewLocations = GetViewLocations( locations, usingModules || !usingAreas ? null : areaLocations, !usingModules ? null : moduleLocations, !usingModules || !usingAreas ? null : moduleAreaLocations); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The property '{0}' cannot be null or empty.", locationsPropertyName)); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = this.CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName, moduleName); if (useCache) { // Only look at cached display modes that can handle the context. string cachedLocation = null; IEnumerable <IDisplayMode> possibleDisplayModes = DisplayModeProvider.GetAvailableDisplayModesForContext(controllerContext.HttpContext, controllerContext.DisplayMode); foreach (IDisplayMode displayMode in possibleDisplayModes) { cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, this.AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId)); if (cachedLocation != null) { if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = displayMode; } break; } } // if cachedLocation is null GetPath will be called again without using the cache. return(cachedLocation); } else { return((nameRepresentsPath) ? this.GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : this.GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, moduleName, cacheKey, ref searchedLocations)); } }
/// <summary> /// 获取文件的路径 /// </summary> private string GetPath(ControllerContext controllerContext, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = null; //视图位置列表 string[] locations = null; //主题 var theme = string.Empty; //获取区域 var area = GetRouteDataTokenValue("area", controllerContext.RouteData.DataTokens).ToLower(); if (area.Length == 0) { theme = GetRouteDataTokenValue("theme", controllerContext.RouteData.DataTokens); //locations = theme.Length == 0 ? _webviewlocationformats : _webviewlocationformats; locations = _webviewlocationformats; } else if (area == "admin") //后台视图位置的处理 { locations = _adminviewlocationformats; } else if (area == "weixin") { locations = _weixinviewlocationformats; } //是否为特殊路径的标识 var flag2 = IsSpecificName(name); //从缓存中获取视图位置 var cacheKey = CreateCacheKey(cacheKeyPrefix, name, flag2 ? string.Empty : controllerName, area, theme); //视图位置的缓存键 if (!useCache) { return(!flag2 ? GetPathFromGeneralName(controllerContext, locations, name, controllerName, theme, cacheKey, ref searchedLocations) : GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations)); } //从缓存中得到视图位置 var cachedPath = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey); if (cachedPath != null) { return(cachedPath); } //如果视图位置不在缓存中,则构建视图位置并存储到缓存中 return(!flag2 ? GetPathFromGeneralName(controllerContext, locations, name, controllerName, theme, cacheKey, ref searchedLocations) : GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (string.IsNullOrEmpty(name)) { return(string.Empty); } var areaName = AreaHelpers.GetAreaName(controllerContext.RouteData); var usingAreas = !string.IsNullOrEmpty(areaName); var viewLocations = GetViewLocations(locations, usingAreas ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, HotcakesResources.Common_PropertyCannotBeNullOrEmpty, locationsPropertyName)); } var nameRepresentsPath = IsSpecificPath(name); var rootFolder = HotcakesApplication.Current.ViewsVirtualPath; var cacheKey = CreateCacheKey(cacheKeyPrefix, name, nameRepresentsPath ? string.Empty : controllerName, areaName, rootFolder); if (useCache) { // Only look at cached display modes that can handle the context. var possibleDisplayModes = DisplayModeProvider.GetAvailableDisplayModesForContext(controllerContext.HttpContext, controllerContext.DisplayMode); foreach (var displayMode in possibleDisplayModes) { var cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId)); if (cachedLocation != null) { if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = displayMode; } return(cachedLocation); } } // GetPath is called again without using the cache. return(null); } return(nameRepresentsPath ? GetPathFromSpecificName(controllerContext, name, rootFolder, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, rootFolder, cacheKey, ref searchedLocations)); }
private string GetPathFromSpecificName(ControllerContext controllerContext, string name, string cacheKey, bool checkPathValidity, ref string[] searchedLocations, ref bool incompleteMatch) { string result = name; bool fileExists = FileExists(controllerContext, name); if (checkPathValidity && fileExists) { bool?validPath = IsValidPath(controllerContext, name); if (validPath == false) { fileExists = false; } else if (validPath == null) { incompleteMatch = true; } } if (!fileExists) { result = string.Empty; searchedLocations = new[] { name }; } #if LOCALIZE else { // View was located, not lets look on that same location for localized versions // of the same view string localizedVirtualPath; if (LocalizedFileExists(controllerContext, result, out localizedVirtualPath)) { result = localizedVirtualPath; if (_onLocalized != null) { _onLocalized(this, new ViewEngines.LocalizedViewEventArgs(controllerContext, result, localizedVirtualPath)); } } else { if (_onLocalized != null) { _onLocalized(this, new ViewEngines.LocalizedViewEventArgs(controllerContext, result)); } } } #endif if (!incompleteMatch) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, result); } return(result); }
private ViewEngineResult NewFindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { // Get the name of the controller from the path string controller = controllerContext.RouteData.Values["controller"].ToString(); string area = ""; try { area = controllerContext.RouteData.DataTokens["area"].ToString(); } catch { } // Create the key for caching purposes string keyPath = Path.Combine(area, controller, viewName); // Try the cache if (useCache) { //If using the cache, check to see if the location is cached. string cacheLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, keyPath); if (!string.IsNullOrWhiteSpace(cacheLocation)) { return(new ViewEngineResult(CreateView(controllerContext, cacheLocation, masterName), this)); } } // Remember the attempted paths, if not found display the attempted paths in the error message. var attempts = new List <string>(); string[] locationFormats = string.IsNullOrEmpty(area) ? ViewLocationFormats : AreaViewLocationFormats; // for each of the paths defined, format the string and see if that path exists. When found, cache it. foreach (string rootPath in locationFormats) { string currentPath = string.IsNullOrEmpty(area) ? string.Format(rootPath, viewName, controller) : string.Format(rootPath, viewName, controller, area); if (FileExists(controllerContext, currentPath)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, currentPath); return(new ViewEngineResult(CreateView(controllerContext, currentPath, masterName), this)); } // If not found, add to the list of attempts. attempts.Add(currentPath); } // if not found by now, simply return the attempted paths. return(new ViewEngineResult(attempts.Distinct().ToList())); }
private string GetPathFromSpecificName(ControllerArgs controllerContext, ViewType viewType, string name) { var virtualPath = name; if (!FileExists(controllerContext, name)) { throw new Exception(virtualPath); } ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, controllerContext.GetChacheKey(viewType, name), virtualPath); return(virtualPath); }