コード例 #1
0
        private ViewLocationResult FindView(IEnumerable <string> locations, string viewName, string fileNameFormat = "{0}", bool throwException = false)
        {
            var allFiles         = this.LoadThemeFiles();
            var checkedLocations = new List <string>();
            var viewFound        = false;

            ViewLocationResult foundView = null;

            foreach (var fullPath in
                     locations.Select(
                         viewLocation => this.RemoveBaseDirectory(Path.Combine(this.ThemeDirectory, viewLocation, string.Format(fileNameFormat, viewName)))))
            {
                foundView = allFiles.SingleOrDefault(x => x.Name.Equals(fullPath, StringComparison.OrdinalIgnoreCase));
                if (foundView != null)
                {
                    viewFound = true;
                    break;
                }

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

            // use more flex method
            if (!viewFound)
            {
                var tempViewName = string.Format(fileNameFormat, viewName);
                if (tempViewName.StartsWith("*"))
                {
                    tempViewName = viewName.RemovePrefix("*");
                    foreach (var location in locations)
                    {
                        var tempLocation = this.RemoveBaseDirectory(
                            Path.Combine(this.ThemeDirectory, location));
                        foundView = allFiles.SingleOrDefault(x => x.Name.EndsWith(tempViewName, StringComparison.OrdinalIgnoreCase) && x.Name.StartsWith(tempLocation, StringComparison.OrdinalIgnoreCase));
                        if (foundView != null)
                        {
                            viewFound = true;
                            break;
                        }
                    }
                }
            }

            // now search in global location
            // App_Data/Themes/_Global
            if (!viewFound)
            {
                foreach (var fullPath in
                         locations.Select(
                             viewLocation => this.RemoveBaseDirectory(Path.Combine("_global", viewLocation, string.Format(fileNameFormat, viewName)))))
                {
                    foundView = allFiles.SingleOrDefault(x => x.Name.Equals(fullPath, StringComparison.OrdinalIgnoreCase));
                    if (foundView != null)
                    {
                        viewFound = true;
                        break;
                    }

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

            // use more flex method to search for asteric
            if (!viewFound)
            {
                var tempViewName = string.Format(fileNameFormat, viewName);
                if (tempViewName.StartsWith("*"))
                {
                    tempViewName = viewName.RemovePrefix("*");
                    foreach (var location in locations)
                    {
                        var tempLocation = this.RemoveBaseDirectory(Path.Combine("_global", location));
                        foundView = allFiles.SingleOrDefault(x => x.Name.EndsWith(tempViewName, StringComparison.OrdinalIgnoreCase) && x.Name.StartsWith(tempLocation, StringComparison.OrdinalIgnoreCase));
                        if (foundView != null)
                        {
                            viewFound = true;
                            break;
                        }
                    }
                }
            }

            if (!viewFound && throwException)
            {
                return(new ViewLocationResult(checkedLocations.ToArray()));
            }

            // since file is stale, make sure to refresh cache contents
            if (foundView != null && foundView.IsStale())
            {
                if (foundView is FileViewLocationResult)
                {
                    lock (this._Lock)
                    {
                        if (!((FileViewLocationResult)foundView).Reload()) // file was deleted
                        {
                            return(null);
                        }
                    }
                }
            }

            return(foundView);
        }