internal static void ExecuteStartPageInternal( HttpApplication application, Action <string> monitorFile, IVirtualPathFactory virtualPathFactory, IEnumerable <string> supportedExtensions ) { ApplicationStartPage startPage = null; foreach (var extension in supportedExtensions) { var vpath = StartPageVirtualPath + extension; // We need to monitor regardless of existence because the user could add/remove the // file at any time. monitorFile(vpath); if (!virtualPathFactory.Exists(vpath)) { continue; } if (startPage == null) { startPage = virtualPathFactory.CreateInstance <ApplicationStartPage>(vpath); startPage.Application = application; startPage.VirtualPathFactory = virtualPathFactory; startPage.ExecuteInternal(); } } }
internal static void ExecuteStartPageInternal(HttpApplication application, Action<string> monitorFile, IVirtualPathFactory virtualPathFactory, IEnumerable<string> supportedExtensions) { ApplicationStartPage startPage = null; foreach (var extension in supportedExtensions) { var vpath = StartPageVirtualPath + extension; // We need to monitor regardless of existence because the user could add/remove the // file at any time. monitorFile(vpath); if (!virtualPathFactory.Exists(vpath)) { continue; } if (startPage == null) { startPage = virtualPathFactory.CreateInstance<ApplicationStartPage>(vpath); startPage.Application = application; startPage.VirtualPathFactory = virtualPathFactory; startPage.ExecuteInternal(); } } }
internal static IHttpHandler CreateFromVirtualPath( string virtualPath, IVirtualPathFactory virtualPathFactory ) { // We will try to create a WebPage from our factory. If this fails, we assume that the virtual path maps to an IHttpHandler. // Instantiate the page from the virtual path WebPage page = virtualPathFactory.CreateInstance <WebPage>(virtualPath); // If it's not a page, assume it's a regular handler if (page == null) { return(virtualPathFactory.CreateInstance <IHttpHandler>(virtualPath)); } // Mark it as a 'top level' page (as opposed to a user control or master) page.TopLevelPage = true; // Give it its virtual path page.VirtualPath = virtualPath; // Assign it the object factory page.VirtualPathFactory = virtualPathFactory; // Return a handler over it return(new WebPageHttpHandler(page)); }
public TResource CreateInstance <TResource>(string virtualPath) { IVirtualPathFactory factory = _factories.FirstOrDefault(f => f.Exists(virtualPath)); if (factory != null) { return((TResource)factory.CreateInstance(virtualPath)); } return((TResource)_defaultFactory(virtualPath, typeof(TResource))); }
internal static WebPageRenderingBase GetStartPage( WebPageRenderingBase page, IVirtualPathFactory virtualPathFactory, string appDomainAppVirtualPath, string fileName, IEnumerable <string> supportedExtensions ) { // Build up a list of pages to execute, such as one of the following: // ~/somepage.cshtml // ~/_pageStart.cshtml --> ~/somepage.cshtml // ~/_pageStart.cshtml --> ~/sub/_pageStart.cshtml --> ~/sub/somepage.cshtml WebPageRenderingBase currentPage = page; var pageDirectory = VirtualPathUtility.GetDirectory(page.VirtualPath); // Start with the requested page's directory, find the init page, // and then traverse up the hierarchy to find init pages all the // way up to the root of the app. while ( !String.IsNullOrEmpty(pageDirectory) && pageDirectory != "/" && PathUtil.IsWithinAppRoot(appDomainAppVirtualPath, pageDirectory) ) { // Go through the list of supported extensions foreach (var extension in supportedExtensions) { var virtualPath = VirtualPathUtility.Combine( pageDirectory, fileName + "." + extension ); // Can we build a file from the current path? if (virtualPathFactory.Exists(virtualPath)) { var parentStartPage = virtualPathFactory.CreateInstance <StartPage>( virtualPath ); parentStartPage.VirtualPath = virtualPath; parentStartPage.ChildPage = currentPage; parentStartPage.VirtualPathFactory = virtualPathFactory; currentPage = parentStartPage; break; } } pageDirectory = currentPage.GetDirectory(pageDirectory); } // At this point 'currentPage' is the root-most StartPage (if there were // any StartPages at all) or it is the requested page itself. return(currentPage); }
internal static void ExecuteStartPage(HttpApplication application, Action<string> monitorFile, IVirtualPathFactory virtualPathFactory, IEnumerable<string> supportedExtensions) { try { ExecuteStartPageInternal(application, monitorFile, virtualPathFactory, supportedExtensions); } catch (Exception e) { // Throw it as a HttpException so as to // display the original stack trace information. Exception = e; throw new HttpException(null, e); } }
public PrecompiledView( string virtualPath, Type type, bool runViewStartPages, IEnumerable <string> fileExtensions, IVirtualPathFactory virtualPathFactory, IViewPageActivator viewPageActivator) { this.type = type; this.virtualPathFactory = virtualPathFactory; this.virtualPath = virtualPath; RunViewStartPages = runViewStartPages; ViewStartFileExtensions = fileExtensions; this.viewPageActivator = viewPageActivator; }
public PrecompiledView( string virtualPath, Type type, bool runViewStartPages, IEnumerable<string> fileExtensions, IVirtualPathFactory virtualPathFactory, IViewPageActivator viewPageActivator) { this.type = type; this.virtualPathFactory = virtualPathFactory; this.virtualPath = virtualPath; RunViewStartPages = runViewStartPages; ViewStartFileExtensions = fileExtensions; this.viewPageActivator = viewPageActivator; }
public static T CreateInstance <T>(this IVirtualPathFactory factory, string virtualPath) where T : class { var virtualPathFactoryManager = factory as VirtualPathFactoryManager; if (virtualPathFactoryManager != null) { return(virtualPathFactoryManager.CreateInstanceOfType <T>(virtualPath)); } var buildManagerFactory = factory as BuildManagerWrapper; if (buildManagerFactory != null) { return(buildManagerFactory.CreateInstanceOfType <T>(virtualPath)); } return(factory.CreateInstance(virtualPath) as T); }
protected override void OnStartProvider() { var property = typeof(VirtualPathFactoryManager).GetProperty("Instance", BindingFlags.NonPublic | BindingFlags.Static); if (property != null) { var member = typeof(VirtualPathFactoryManager).GetField("_virtualPathFactories", BindingFlags.NonPublic | BindingFlags.Instance); if (member != null) { var instance = (VirtualPathFactoryManager)property.GetValue(null); var list = (LinkedList <IVirtualPathFactory>)member.GetValue(instance); var factory = list.First.Value; _previousPathFactory = factory; VirtualPathFactoryManager.RegisterVirtualPathFactory(this); } } }
internal static WebPageRenderingBase GetStartPage(WebPageRenderingBase page, IVirtualPathFactory virtualPathFactory, string appDomainAppVirtualPath, string fileName, IEnumerable<string> supportedExtensions) { // Build up a list of pages to execute, such as one of the following: // ~/somepage.cshtml // ~/_pageStart.cshtml --> ~/somepage.cshtml // ~/_pageStart.cshtml --> ~/sub/_pageStart.cshtml --> ~/sub/somepage.cshtml WebPageRenderingBase currentPage = page; var pageDirectory = VirtualPathUtility.GetDirectory(page.VirtualPath); // Start with the requested page's directory, find the init page, // and then traverse up the hierarchy to find init pages all the // way up to the root of the app. while (!string.IsNullOrEmpty(pageDirectory)) { // Go through the list of supported extensions foreach (var extension in supportedExtensions) { var virtualPath = VirtualPathUtility.Combine(pageDirectory, fileName + "." + extension); // Can we build a file from the current path? if (virtualPathFactory.Exists(virtualPath)) { var parentStartPage = virtualPathFactory.CreateInstance(virtualPath) as StartPage; parentStartPage.VirtualPath = virtualPath; parentStartPage.ChildPage = currentPage; parentStartPage.VirtualPathFactory = virtualPathFactory; currentPage = parentStartPage; break; } } pageDirectory = pageDirectory == "~/" ? null : VirtualPathUtility.GetDirectory(pageDirectory); } // At this point 'currentPage' is the root-most StartPage (if there were // any StartPages at all) or it is the requested page itself. return currentPage; }
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; }
internal static void ExecuteStartPage( HttpApplication application, Action <string> monitorFile, IVirtualPathFactory virtualPathFactory, IEnumerable <string> supportedExtensions ) { try { ExecuteStartPageInternal( application, monitorFile, virtualPathFactory, supportedExtensions ); } catch (Exception e) { // Throw it as a HttpException so as to // display the original stack trace information. Exception = e; throw new HttpException(null, e); } }
internal static WebPageBase CreateInstanceFromVirtualPath( string virtualPath, IVirtualPathFactory virtualPathFactory ) { // Get the compiled object try { WebPageBase webPage = virtualPathFactory.CreateInstance <WebPageBase>(virtualPath); // Give it its virtual path webPage.VirtualPath = virtualPath; // Assign it the VirtualPathFactory webPage.VirtualPathFactory = virtualPathFactory; return(webPage); } catch (HttpException e) { BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, e); throw; } }
internal static IHttpHandler CreateFromVirtualPath(string virtualPath, IVirtualPathFactory virtualPathFactory) { // We will try to create a WebPage from our factory. If this fails, we assume that the virtual path maps to an IHttpHandler. // Instantiate the page from the virtual path WebPage page = virtualPathFactory.CreateInstance<WebPage>(virtualPath); // If it's not a page, assume it's a regular handler if (page == null) { return virtualPathFactory.CreateInstance<IHttpHandler>(virtualPath); } // Mark it as a 'top level' page (as opposed to a user control or master) page.TopLevelPage = true; // Give it its virtual path page.VirtualPath = virtualPath; // Assign it the object factory page.VirtualPathFactory = virtualPathFactory; // Return a handler over it return new WebPageHttpHandler(page); }
internal void RegisterVirtualPathFactoryInternal(IVirtualPathFactory virtualPathFactory) { _virtualPathFactories.AddBefore(_virtualPathFactories.Last, virtualPathFactory); }
public static void Start(MarkdownPagesOptions options, IVirtualPathFactory virtualPathFactory) { WebPageHttpHandlerExtensions.RegisterExtensions(options.MarkdownExtensions.Select(ext => ext.RemoveLeadingDot())); VirtualPathFactoryManager.RegisterVirtualPathFactory(virtualPathFactory); }
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 RegisterFactory(IVirtualPathFactory factory) { Instance.Factories.Add(factory); }
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 bool FileExists(string virtualPath, IVirtualPathFactory virtualPathFactory) { var path = "~/" + virtualPath; return(virtualPathFactory.Exists(path)); }
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)); }
internal static WebPageBase CreateInstanceFromVirtualPath(string virtualPath, IVirtualPathFactory virtualPathFactory) { try { WebPageBase instance = VirtualPathFactoryExtensions.CreateInstance<WebPageBase>(virtualPathFactory, virtualPath); instance.VirtualPath = virtualPath; instance.VirtualPathFactory = virtualPathFactory; return instance; } catch (HttpException ex) { BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, ex); throw; } }
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; }
private static bool FileExists(string virtualPath, IVirtualPathFactory virtualPathFactory) { var path = "~/" + virtualPath; return virtualPathFactory.Exists(path); }
internal static WebPageBase CreateInstanceFromVirtualPath(string virtualPath, IVirtualPathFactory virtualPathFactory) { // Get the compiled object try { WebPageBase webPage = virtualPathFactory.CreateInstance<WebPageBase>(virtualPath); // Give it its virtual path webPage.VirtualPath = virtualPath; // Assign it the VirtualPathFactory webPage.VirtualPathFactory = virtualPathFactory; return webPage; } catch (HttpException e) { BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, e); throw; } }
internal void RegisterVirtualPathFactoryInternal(IVirtualPathFactory virtualPathFactory) { _virtualPathFactories.Add(virtualPathFactory); }
public PrecompiledVirtualPathFactory(Func<string, Type> pathLookup, IVirtualPathFactory backup) { PathLookup = pathLookup; Backup = backup; }
internal VirtualPathFactoryManager(IVirtualPathFactory defaultFactory) { _virtualPathFactories.AddFirst(defaultFactory); }
public PrecompiledVirtualPathFactory(Func <string, Type> pathLookup, IVirtualPathFactory backup) { PathLookup = pathLookup; Backup = backup; }
public static void RegisterVirtualPathFactory(IVirtualPathFactory virtualPathFactory) { Instance.RegisterVirtualPathFactoryInternal(virtualPathFactory); }