/// <summary> /// Attempts to create a WebPageBase instance from a virtualPath and wraps complex compiler exceptions with simpler messages /// </summary> protected virtual WebPageBase CreatePageFromVirtualPath( string virtualPath, HttpContextBase httpContext, Func <string, bool> virtualPathExists, DisplayModeProvider displayModeProvider, IDisplayMode displayMode ) { try { DisplayInfo resolvedDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath( virtualPath, httpContext, virtualPathExists, displayMode ); if (resolvedDisplayInfo != null) { var webPage = VirtualPathFactory.CreateInstance <WebPageBase>( resolvedDisplayInfo.FilePath ); if (webPage != null) { // Give it its virtual path webPage.VirtualPath = virtualPath; webPage.VirtualPathFactory = VirtualPathFactory; webPage.DisplayModeProvider = DisplayModeProvider; return(webPage); } } } catch (HttpException e) { // If the path uses an unregistered extension, such as Foo.txt, // then an error regarding build providers will be thrown. // Check if this is the case and throw a simpler error. BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, e); // If the path uses an extension registered with codedom, such as Foo.js, // then an unfriendly compilation error might get thrown by the underlying compiler. // Check if this is the case and throw a simpler error. BuildManagerExceptionUtil.ThrowIfCodeDomDefinedExtension(virtualPath, e); // Rethrow any errors throw; } // The page is missing, could not be compiled or is of an invalid type. throw new HttpException( String.Format( CultureInfo.CurrentCulture, WebPageResources.WebPage_InvalidPageType, virtualPath ) ); }
private static WebPageMatch MatchDefaultFiles( string pathValue, string[] supportedExtensions, Func <string, bool> virtualPathExists, HttpContextBase context, DisplayModeProvider displayModes, string currentLevel ) { // If we haven't found anything yet, now try looking for default.* or index.* at the current url currentLevel = pathValue; string currentLevelDefault; string currentLevelIndex; if (String.IsNullOrEmpty(currentLevel)) { currentLevelDefault = "default"; currentLevelIndex = "index"; } else { if (currentLevel[currentLevel.Length - 1] != '/') { currentLevel += "/"; } currentLevelDefault = currentLevel + "default"; currentLevelIndex = currentLevel + "index"; } // Does the current route level match any supported extension? string defaultMatch = GetRouteLevelMatch( currentLevelDefault, supportedExtensions, virtualPathExists, context, displayModes ); if (defaultMatch != null) { return(new WebPageMatch(defaultMatch, String.Empty)); } string indexMatch = GetRouteLevelMatch( currentLevelIndex, supportedExtensions, virtualPathExists, context, displayModes ); if (indexMatch != null) { return(new WebPageMatch(indexMatch, String.Empty)); } return(null); }
private static string GetRouteLevelMatch(string pathValue, string[] supportedExtensions, Func <string, bool> virtualPathExists, HttpContextBase context, DisplayModeProvider displayModeProvider) { for (int i = 0; i < supportedExtensions.Length; i++) { string supportedExtension = supportedExtensions[i]; // For performance, avoid multiple calls to String.Concat string virtualPath; // Only add the extension if it's not already there if (!PathHelpers.EndsWithExtension(pathValue, supportedExtension)) { virtualPath = "~/" + pathValue + "." + supportedExtension; } else { virtualPath = "~/" + pathValue; } DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathExists, currentDisplayMode: null); if (virtualPathDisplayInfo != null) { // If there's an exact match on disk, return it unless it starts with an underscore if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase)) { throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked); } string resolvedVirtualPath = virtualPathDisplayInfo.FilePath; // Matches are not expected to be virtual paths so remove the ~/ from the match if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase)) { resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2); } DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode); return(resolvedVirtualPath); } } return(null); }
public static void Register(DisplayModeProvider instance) { var categorizr = new Categorizr.Impl.Categorizr(); instance.Modes.Insert(0, new DefaultDisplayMode("Mobile") { ContextCondition = c => categorizr.Detect(c.GetOverriddenUserAgent()).IsMobile }); instance.Modes.Insert(1, new DefaultDisplayMode("Tablet") { ContextCondition = c => categorizr.Detect(c.GetOverriddenUserAgent()).IsTablet }); instance.Modes.Insert(2, new DefaultDisplayMode("TV") { ContextCondition = c => categorizr.Detect(c.GetOverriddenUserAgent()).IsTv }); }
private static string GetRouteLevelMatch(string pathValue, string[] supportedExtensions, Func<string, bool> virtualPathExists, HttpContextBase context, DisplayModeProvider displayModeProvider) { for (int i = 0; i < supportedExtensions.Length; i++) { string supportedExtension = supportedExtensions[i]; // For performance, avoid multiple calls to String.Concat string virtualPath; // Only add the extension if it's not already there if (!PathHelpers.EndsWithExtension(pathValue, supportedExtension)) { virtualPath = "~/" + pathValue + "." + supportedExtension; } else { virtualPath = "~/" + pathValue; } DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathExists, currentDisplayMode: null); if (virtualPathDisplayInfo != null) { // If there's an exact match on disk, return it unless it starts with an underscore if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase)) { throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked); } string resolvedVirtualPath = virtualPathDisplayInfo.FilePath; // Matches are not expected to be virtual paths so remove the ~/ from the match if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase)) { resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2); } DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode); return resolvedVirtualPath; } } return null; }
private static string GetRouteLevelMatch(string pathValue, IEnumerable <string> supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModeProvider) { foreach (string supportedExtension in supportedExtensions) { string virtualPath = "~/" + pathValue; // Only add the extension if it's not already there if (!virtualPath.EndsWith("." + supportedExtension, StringComparison.OrdinalIgnoreCase)) { virtualPath += "." + supportedExtension; } DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathFactory.Exists, currentDisplayMode: null); if (virtualPathDisplayInfo != null) { // If there's an exact match on disk, return it unless it starts with an underscore if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase)) { throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked); } string resolvedVirtualPath = virtualPathDisplayInfo.FilePath; // Matches are not expected to be virtual paths so remove the ~/ from the match if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase)) { resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2); } DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode); return(resolvedVirtualPath); } } return(null); }
private static string GetRouteLevelMatch(string pathValue, IEnumerable<string> supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModeProvider) { foreach (string supportedExtension in supportedExtensions) { string virtualPath = "~/" + pathValue; // Only add the extension if it's not already there if (!virtualPath.EndsWith("." + supportedExtension, StringComparison.OrdinalIgnoreCase)) { virtualPath += "." + supportedExtension; } DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathFactory.Exists, currentDisplayMode: null); if (virtualPathDisplayInfo != null) { // If there's an exact match on disk, return it unless it starts with an underscore if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase)) { throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked); } string resolvedVirtualPath = virtualPathDisplayInfo.FilePath; // Matches are not expected to be virtual paths so remove the ~/ from the match if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase)) { resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2); } DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode); return resolvedVirtualPath; } } return null; }
protected virtual string GetPathFromGeneralName(ControllerContext controllerContext, List <ThemeableVirtualPathProviderViewEngine.ViewLocation> locations, string name, string controllerName, string areaName, string cacheKey, ref string[] searchedLocations) { Func <string, bool> func; Func <string, bool> func1 = null; Func <string, bool> func2 = null; string empty = string.Empty; searchedLocations = new string[locations.Count]; int num = 0; while (num < locations.Count) { ThemeableVirtualPathProviderViewEngine.ViewLocation item = locations[num]; string str = ""; str = item.Format(name, controllerName, areaName); if (!File.Exists(HttpContext.Current.Server.MapPath(str))) { str = item.Format(name, controllerName, areaName); } System.Web.WebPages.DisplayModeProvider displayModeProvider = base.DisplayModeProvider; string str1 = str; HttpContextBase httpContext = controllerContext.HttpContext; Func <string, bool> func3 = func1; if (func3 == null) { Func <string, bool> func4 = (string path) => this.FileExists(controllerContext, path); func = func4; func1 = func4; func3 = func; } DisplayInfo displayInfoForVirtualPath = displayModeProvider.GetDisplayInfoForVirtualPath(str1, httpContext, func3, controllerContext.DisplayMode); if (displayInfoForVirtualPath == null) { searchedLocations[num] = str; num++; } else { string filePath = displayInfoForVirtualPath.FilePath; searchedLocations = ThemeableVirtualPathProviderViewEngine._emptyLocations; empty = filePath; base.ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, this.AppendDisplayModeToCacheKey(cacheKey, displayInfoForVirtualPath.DisplayMode.DisplayModeId), empty); if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = displayInfoForVirtualPath.DisplayMode; } foreach (IDisplayMode mode in base.DisplayModeProvider.Modes) { if (mode.DisplayModeId != displayInfoForVirtualPath.DisplayMode.DisplayModeId) { IDisplayMode displayMode = mode; HttpContextBase httpContextBase = controllerContext.HttpContext; string str2 = str; Func <string, bool> func5 = func2; if (func5 == null) { Func <string, bool> func6 = (string path) => this.FileExists(controllerContext, path); func = func6; func2 = func6; func5 = func; } DisplayInfo displayInfo = displayMode.GetDisplayInfo(httpContextBase, str2, func5); string empty1 = string.Empty; if ((displayInfo == null ? false : displayInfo.FilePath != null)) { empty1 = displayInfo.FilePath; } base.ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, this.AppendDisplayModeToCacheKey(cacheKey, mode.DisplayModeId), empty1); } } break; } } return(empty); }
internal static WebPageMatch MatchRequest(string pathValue, IEnumerable <string> supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModes) { string currentLevel = String.Empty; string currentPathInfo = pathValue; // We can skip the file exists check and normal lookup for empty paths, but we still need to look for default pages if (!String.IsNullOrEmpty(pathValue)) { // If the file exists and its not a supported extension, let the request go through if (FileExists(pathValue, virtualPathFactory)) { // TODO: Look into switching to RawURL to eliminate the need for this issue bool foundSupportedExtension = false; foreach (string supportedExtension in supportedExtensions) { if (pathValue.EndsWith("." + supportedExtension, StringComparison.OrdinalIgnoreCase)) { foundSupportedExtension = true; break; } } if (!foundSupportedExtension) { return(null); } } // For each trimmed part of the path try to add a known extension and // check if it matches a file in the application. currentLevel = pathValue; currentPathInfo = String.Empty; while (true) { // Does the current route level patch any supported extension? string routeLevelMatch = GetRouteLevelMatch(currentLevel, supportedExtensions, virtualPathFactory, context, displayModes); if (routeLevelMatch != null) { return(new WebPageMatch(routeLevelMatch, currentPathInfo)); } // Try to remove the last path segment (e.g. go from /foo/bar to /foo) int indexOfLastSlash = currentLevel.LastIndexOf('/'); if (indexOfLastSlash == -1) { // If there are no more slashes, we're done break; } else { // Chop off the last path segment to get to the next one currentLevel = currentLevel.Substring(0, indexOfLastSlash); // And save the path info in case there is a match currentPathInfo = pathValue.Substring(indexOfLastSlash + 1); } } } return(MatchDefaultFiles(pathValue, supportedExtensions, virtualPathFactory, context, displayModes, currentLevel)); }
private WebPageBase CreatePageFromVirtualPath(string virtualPath, HttpContextBase httpContext, Func<string, bool> virtualPathExists, DisplayModeProvider displayModeProvider, IDisplayMode displayMode) { try { DisplayInfo infoForVirtualPath = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, httpContext, virtualPathExists, displayMode); if (infoForVirtualPath != null) { WebPageBase instance = VirtualPathFactoryExtensions.CreateInstance<WebPageBase>(this.VirtualPathFactory, infoForVirtualPath.FilePath); if (instance != null) { instance.VirtualPath = virtualPath; instance.VirtualPathFactory = this.VirtualPathFactory; instance.DisplayModeProvider = this.DisplayModeProvider; return instance; } } } catch (HttpException ex) { BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, ex); BuildManagerExceptionUtil.ThrowIfCodeDomDefinedExtension(virtualPath, ex); throw; } throw new HttpException(string.Format((IFormatProvider) CultureInfo.CurrentCulture, WebPageResources.WebPage_InvalidPageType, new object[1] { (object) virtualPath })); }
/// <summary> /// Attempts to create a WebPageBase instance from a virtualPath and wraps complex compiler exceptions with simpler messages /// </summary> private WebPageBase CreatePageFromVirtualPath(string virtualPath, HttpContextBase httpContext, Func<string, bool> virtualPathExists, DisplayModeProvider displayModeProvider, IDisplayMode displayMode) { try { DisplayInfo resolvedDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, httpContext, virtualPathExists, displayMode); if (resolvedDisplayInfo != null) { var webPage = VirtualPathFactory.CreateInstance<WebPageBase>(resolvedDisplayInfo.FilePath); if (webPage != null) { // Give it its virtual path webPage.VirtualPath = virtualPath; webPage.VirtualPathFactory = VirtualPathFactory; webPage.DisplayModeProvider = DisplayModeProvider; return webPage; } } } catch (HttpException e) { // If the path uses an unregistered extension, such as Foo.txt, // then an error regarding build providers will be thrown. // Check if this is the case and throw a simpler error. BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, e); // If the path uses an extension registered with codedom, such as Foo.js, // then an unfriendly compilation error might get thrown by the underlying compiler. // Check if this is the case and throw a simpler error. BuildManagerExceptionUtil.ThrowIfCodeDomDefinedExtension(virtualPath, e); // Rethrow any errors throw; } // The page is missing, could not be compiled or is of an invalid type. throw new HttpException(String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_InvalidPageType, virtualPath)); }
private static WebPageMatch MatchDefaultFiles(string pathValue, IEnumerable<string> supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModes, string currentLevel) { // If we haven't found anything yet, now try looking for default.* or index.* at the current url currentLevel = pathValue; string currentLevelDefault; string currentLevelIndex; if (String.IsNullOrEmpty(currentLevel)) { currentLevelDefault = "default"; currentLevelIndex = "index"; } else { if (currentLevel[currentLevel.Length - 1] != '/') { currentLevel += "/"; } currentLevelDefault = currentLevel + "default"; currentLevelIndex = currentLevel + "index"; } // Does the current route level match any supported extension? string defaultMatch = GetRouteLevelMatch(currentLevelDefault, supportedExtensions, virtualPathFactory, context, displayModes); if (defaultMatch != null) { return new WebPageMatch(defaultMatch, String.Empty); } string indexMatch = GetRouteLevelMatch(currentLevelIndex, supportedExtensions, virtualPathFactory, context, displayModes); if (indexMatch != null) { return new WebPageMatch(indexMatch, String.Empty); } return null; }
internal static WebPageMatch MatchRequest(string pathValue, IEnumerable<string> supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModes) { string currentLevel = String.Empty; string currentPathInfo = pathValue; // We can skip the file exists check and normal lookup for empty paths, but we still need to look for default pages if (!String.IsNullOrEmpty(pathValue)) { // If the file exists and its not a supported extension, let the request go through if (FileExists(pathValue, virtualPathFactory)) { // TODO: Look into switching to RawURL to eliminate the need for this issue bool foundSupportedExtension = false; foreach (string supportedExtension in supportedExtensions) { if (pathValue.EndsWith("." + supportedExtension, StringComparison.OrdinalIgnoreCase)) { foundSupportedExtension = true; break; } } if (!foundSupportedExtension) { return null; } } // For each trimmed part of the path try to add a known extension and // check if it matches a file in the application. currentLevel = pathValue; currentPathInfo = String.Empty; while (true) { // Does the current route level patch any supported extension? string routeLevelMatch = GetRouteLevelMatch(currentLevel, supportedExtensions, virtualPathFactory, context, displayModes); if (routeLevelMatch != null) { return new WebPageMatch(routeLevelMatch, currentPathInfo); } // Try to remove the last path segment (e.g. go from /foo/bar to /foo) int indexOfLastSlash = currentLevel.LastIndexOf('/'); if (indexOfLastSlash == -1) { // If there are no more slashes, we're done break; } else { // Chop off the last path segment to get to the next one currentLevel = currentLevel.Substring(0, indexOfLastSlash); // And save the path info in case there is a match currentPathInfo = pathValue.Substring(indexOfLastSlash + 1); } } } return MatchDefaultFiles(pathValue, supportedExtensions, virtualPathFactory, context, displayModes, currentLevel); }
public static void RegisterUserAgents(DisplayModeProvider displayModeProvider) { }