protected virtual 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 = GetAreaName(controllerContext.RouteData); //little hack to get admin area to be in /Administration/ instead of /Spinx/Admin/ or Areas/Admin/ //if (!string.IsNullOrEmpty(areaName) && areaName.Equals("admin", StringComparison.InvariantCultureIgnoreCase)) //{ // //var newLocations = areaLocations.ToList(); // var newLocations = new List<string>(); // newLocations.Insert(0, "~/Administration/Views/{1}/{0}.cshtml"); // newLocations.Insert(0, "~/Administration/Views/Shared/{0}.cshtml"); // areaLocations = newLocations.ToArray(); //} var flag = !string.IsNullOrEmpty(areaName); var viewLocations = GetViewLocations(locations, flag ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Properties cannot be null or empty.", new object[] { locationsPropertyName })); } var flag2 = IsSpecificPath(name); var key = CreateCacheKey(cacheKeyPrefix, name, flag2 ? string.Empty : controllerName, areaName); if (useCache) { var cached = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key); if (cached != null) { return(cached); } } return(!flag2?GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, key, ref searchedLocations) : GetPathFromSpecificName(controllerContext, name, key, 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 = GetAreaName(controllerContext.RouteData); string theme = controllerContext.HttpContext.GetCurrentTheme(); //controllerContext.HttpContext.Items["theme"] as string; if (string.IsNullOrEmpty(theme)) { theme = "default"; } bool usingAreas = !String.IsNullOrEmpty(areaName); 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, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, theme, cacheKey, ref searchedLocations)); }
private string GetPath(ControllerContext controllerContext, string viewName, string cacheKeyPrefix, bool useCache, string[] viewLocationFormats, string[] viewAreaLocationFormats, out string[] searchedLocations) { searchedLocations = null; if (string.IsNullOrEmpty(viewName)) { return(string.Empty); } string areaName = GetAreaName(controllerContext.RouteData); string controllerName = controllerContext.RouteData.GetRequiredString("controller"); List <ViewLocation> viewLocations = GetViewLocations(viewLocationFormats, !string.IsNullOrEmpty(areaName) ? viewAreaLocationFormats : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Properties cannot be null or empty.", new object[] { cacheKeyPrefix })); } bool isSpecificPath = IsSpecificPath(viewName); string key = this.CreateCacheKey(cacheKeyPrefix, viewName, isSpecificPath ? string.Empty : controllerName, areaName, CacheHelper.Settings.Theme); // return if there is a cache if (useCache) { var cached = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key); if (cached != null) { return(cached); } } // return the view if (isSpecificPath) { return(GetPathFromSpecificName(controllerContext, viewName, key, ref searchedLocations)); } return(GetPathFromGeneralName(controllerContext, viewName, controllerName, areaName, key, viewLocations, ref searchedLocations)); }
private string GetPath( ControllerContext controllerContext, IList <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.Count == 0) { throw new InvalidOperationException("{0} Property cannot be null.".ToFormat(locationsPropertyName)); } var nameRepresentsPath = IsSpecificPath(name); var cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, Thread.CurrentThread.CurrentUICulture.Name); if (useCache) { var 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)); }
protected 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.IsNullOrWhiteSpace(name)) { return(String.Empty); } object areaObj = null; string areaName = string.Empty; controllerContext.RouteData.DataTokens.TryGetValue("area", out areaObj); if (areaObj != null) { areaName = areaObj.ToString(); } bool usingAreas = !String.IsNullOrWhiteSpace(areaName); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, "Property {0} Cannot be NullOrEmpty", 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)); }
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)); }
private string GetPath(ControllerArgs controllerContext, ViewType viewType, string name, bool useCache) { if (string.IsNullOrEmpty(name)) { throw new Exception("Имя вида не задано"); } if (!useCache) { return(IsSpecificPath(name) ? GetPathFromSpecificName(controllerContext, viewType, name) : GetPathFromGeneralName(controllerContext, viewType, name)); } string key = controllerContext.GetChacheKey(viewType, name); var viewLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key); return(viewLocation ?? (IsSpecificPath(name) ? GetPathFromSpecificName(controllerContext, viewType, name) : GetPathFromGeneralName(controllerContext, viewType, name))); }
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 = GetAreaName(controllerContext.RouteData); //little hack to get nop's admin area to be in /Administration/ instead of /Nop/Admin/ or Areas/Admin/ if (!string.IsNullOrEmpty(areaName) && areaName.Equals("admin", StringComparison.InvariantCultureIgnoreCase)) { var newLocations = areaLocations.ToList(); newLocations.Insert(0, "~/Administration/Views/{1}/{0}.cshtml"); newLocations.Insert(0, "~/Administration/Views/Shared/{0}.cshtml"); areaLocations = newLocations.ToArray(); } bool usingAreas = !String.IsNullOrEmpty(areaName); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "", 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)); } }
private ViewEngineResult NewFindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache, bool isPartialView) { if (!ContextCondition(controllerContext.HttpContext)) { return(new ViewEngineResult(new string[] { })); // we found nothing and we pretend we looked nowhere } // 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 { } // Apply the view modifier var newViewName = string.Format("{0}.{1}", viewName, ViewModifier); // Create the key for caching purposes string keyPath = Path.Combine(area, controller, newViewName); string cacheLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, keyPath); // Try the cache if (useCache) { //If using the cache, check to see if the location is cached. if (!string.IsNullOrWhiteSpace(cacheLocation)) { if (isPartialView) { return(new ViewEngineResult(CreatePartialView(controllerContext, cacheLocation), this)); } else { return(new ViewEngineResult(CreateView(controllerContext, cacheLocation, masterName), this)); } } } 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, newViewName, controller) : string.Format(rootPath, newViewName, controller, area); if (FileExists(controllerContext, currentPath)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, currentPath); if (isPartialView) { return(new ViewEngineResult(CreatePartialView(controllerContext, currentPath), this)); } else { return(new ViewEngineResult(CreateView(controllerContext, currentPath, masterName), this)); } } } return(new ViewEngineResult(new string[] { })); // we found nothing and we pretend we looked nowhere }
protected virtual string ResolveViewPath( ControllerContext controllerContext, string areaName, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string theme, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } bool usingAreas = !String.IsNullOrEmpty(areaName); if (usingAreas) { var isAdminArea = areaName.IsCaseInsensitiveEqual("admin"); // "ExtraAreaViewLocations" gets injected by AdminThemedAttribute var extraAreaViewLocations = controllerContext.RouteData.DataTokens["ExtraAreaViewLocations"] as string[]; if (extraAreaViewLocations != null && extraAreaViewLocations.Length > 0) { var newLocations = areaLocations.ToList(); var viewType = cacheKeyPrefix == "Partial" ? ViewType.Partial : ViewType.Layout; if (isAdminArea) { // the admin area cannot fallback to itself. Prepend to list. ExpandLocationFormats(extraAreaViewLocations, viewType).Reverse().Each(x => newLocations.Insert(0, x)); } else { newLocations.AddRange(ExpandLocationFormats(extraAreaViewLocations, viewType)); } areaLocations = newLocations.ToArray(); } } var viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Properties cannot be null or empty.", new object[] { locationsPropertyName })); } bool nameRepresentsPath = IsSpecificPath(name); string cacheKey = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName, theme); 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) { 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. } // ResolveViewPath is called again without using the cache. return(null); } else { return(nameRepresentsPath ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, theme, cacheKey, ref searchedLocations)); } }
private ViewEngineResult FindView(AreaRoute areaRoute, ControllerContext controllerContext, string viewName, bool useCache, bool isPartialView, string masterName = null) { // Get the name of the controller from the path string controller = controllerContext.RouteData.Values["controller"].ToString(); string controllerspaces = controllerContext.RouteData.DataTokens["Space"] as string; string viewSpaces = string.Empty; //检测命名空间, 识别第二代MVC结构 //if (namespaces != null && namespaces.Length > 0) //{ // foreach (string ns in namespaces) // { // if (!ns.EndsWith(".*") && !ns.EndsWith(".Controllers")) // { // controllerFolder = ns.Substring(ns.LastIndexOf('.') + 1); // break; // } // } //} if (!string.IsNullOrWhiteSpace(controllerspaces)) { viewSpaces = controllerspaces; } string area = areaRoute.ToString(); //DONE: 非空时标识使用自定义引擎 子引擎的缓存差异标识 注册子引擎路由时追加, 此参数在回调时回传 string tmpTag = SubAreaViewEngine.SubEngineMap.ContainsKey(areaRoute) ? SubAreaViewEngine.GetTagString(areaRoute, controllerContext) : string.Empty; // Create the key for caching purposes string keyPath = System.IO.Path.Combine(area, controller, MainThemeName, viewSpaces, viewName, tmpTag); // 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(isPartialView ? new ViewEngineResult(CreatePartialView(controllerContext, cacheLocation), this) : 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>(); // Check ViewName 是否是具体路径 具体路径时, 不再以FormatViewPath方式查找 bool isSpecificPath = viewName.StartsWith("~") || viewName.StartsWith("/"); if (isSpecificPath) { if (FileExists(controllerContext, viewName)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, viewName); return(isPartialView ? new ViewEngineResult(CreatePartialView(controllerContext, viewName), this) : new ViewEngineResult(CreateView(controllerContext, viewName, masterName), this)); } // If not found, add to the list of attempts. attempts.Add(viewName); } else { string[] locationFormats; if (areaRoute == AreaRoute.None) { locationFormats = ViewLocationFormats; } else if (SubAreaViewEngine.SubEngineMap.ContainsKey(areaRoute) && !string.IsNullOrWhiteSpace(tmpTag)) { locationFormats = SubAreaViewEngine.SubEngineMap[areaRoute](controllerContext, BaseAreaViewLocationFormat, tmpTag); } else if (SubAreaViewEngine.SubEngineMap.ContainsKey(areaRoute)) { locationFormats = SubAreaViewEngine.SubEngineMap[areaRoute](controllerContext, BaseAreaViewLocationFormat, string.Empty); } else { locationFormats = 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; if (areaRoute == AreaRoute.None) { //默认模版路径 currentPath = string.Format(rootPath, viewName, controller); } else if (string.IsNullOrWhiteSpace(viewSpaces)) { //第一代MVC(默认)路径 currentPath = string.Format(rootPath, viewName, controller, area); } else { //第二代MVC路径 currentPath = string.Format(rootPath, controller + "/" + viewName, viewSpaces, area); } if (FileExists(controllerContext, currentPath)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, currentPath); return(isPartialView ? new ViewEngineResult(CreatePartialView(controllerContext, currentPath), this) : 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 GetPath(ControllerContext controllerContext, IEnumerable <string> locations, IEnumerable <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 = GetAreaName(controllerContext.RouteData); bool usingAreas = !string.IsNullOrEmpty(areaName); string plugin = (string)controllerContext.RouteData.Values["plugin"]; string theme = LS.Get <Settings>().FirstOrDefault().Themes; if (controllerContext.HttpContext.Items["ShopTheme"] != null) { theme = (string)controllerContext.HttpContext.Items["ShopTheme"]; } 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, plugin, 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) { if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = displayMode; } string cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId)); if (cachedLocation != null) { return(cachedLocation); } } // 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, theme, plugin, cacheKey, ref searchedLocations)); } }
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; }
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 string GetPath(ControllerContext controllerContext, string[] locations, string[] namespaceLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) { return(String.Empty); } string nameSpace = NamespaceHelpers.GetNameSpaceWithNotControllers(controllerContext.RouteData); bool usingNameSpace = !string.IsNullOrEmpty(nameSpace); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingNameSpace) ? namespaceLocations : 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(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, nameSpace); 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, nameSpace, cacheKey, ref searchedLocations)); } }
protected virtual 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 assembilesname = Assembly.GetAssembly(controllerContext.Controller.GetType()).GetName().Name; string areaName = GetAreaName(controllerContext.RouteData); areaName = (areaName == null ? (controllerContext.RouteData.DataTokens["Namespaces"] as string[])[0].Replace(".Controllers", "") : areaName); var newLocations = areaLocations.ToList(); var typeAgent = EngineContext.Current.Resolve <ITypeFinder>(); newLocations.Insert(0, "~/Applications/" + assembilesname + "/Views/{1}/{0}.cshtml"); newLocations.Insert(0, "~/Applications/" + assembilesname + "/Views/Shared/{0}.cshtml"); areaLocations = newLocations.ToArray(); bool usingAreas = !String.IsNullOrEmpty(areaName); List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null); if (viewLocations.Count == 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "Properties cannot be null or empty - {0}", 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(nameRepresentsPath ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations)); } // 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)); } }
/// <summary> /// 获取文件的路径 /// </summary> private string GetPath(ControllerContext controllerContext, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations) { searchedLocations = null; //视图位置列表 string[] locations = null; //主题 string theme = string.Empty; //获取区域 string area = GetRouteDataTokenValue("area", controllerContext.RouteData.DataTokens).ToLower(); if (area.Length == 0)//PC商城前台视图位置的处理 { theme = GetRouteDataTokenValue("theme", controllerContext.RouteData.DataTokens); if (theme.Length == 0)//商城页面 { locations = _webviewlocationformats; } else//店铺页面 { locations = _storewebviewlocationformats; } } else if (area == "mob")//移动商城前台视图位置的处理 { locations = _mobileviewlocationformats; } else if (area == "app")//App商城前台视图位置的处理 { locations = _appviewlocationformats; } else if (area == "storeadmin")//PC店铺后台视图位置的处理 { locations = _storeadminviewlocationformats; } else if (area == "malladmin")//PC商城后台视图位置的处理 { locations = _malladminviewlocationformats; } //是否为特殊路径的标识 bool flag2 = IsSpecificName(name); //从缓存中获取视图位置 string cacheKey = CreateCacheKey(cacheKeyPrefix, name, flag2 ? string.Empty : controllerName, area, theme);//视图位置的缓存键 if (useCache) { //从缓存中得到视图位置 var cachedPath = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey); if (cachedPath != null) { return(cachedPath); } } //如果视图位置不在缓存中,则构建视图位置并存储到缓存中 if (!flag2)//不是特殊路径时的操作 { return(GetPathFromGeneralName(controllerContext, locations, name, controllerName, theme, cacheKey, ref searchedLocations)); } else//特殊路径时的操作 { return(GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations)); } }