Example #1
0
        private static void InitApplicationParts()
        {
            // Register the virtual path factory
            var virtualPathFactory = new DictionaryBasedVirtualPathFactory();

            VirtualPathFactoryManager.RegisterVirtualPathFactory(virtualPathFactory);

            // Intantiate the part registry
            _partRegistry = new ApplicationPartRegistry(virtualPathFactory);

            // Register the resource route
            RouteTable.Routes.Add(new Route(ResourceRoute, new ResourceRouteHandler(_partRegistry)));
        }
        internal static IHttpHandler CreateFromVirtualPath(string virtualPath, VirtualPathFactoryManager virtualPathFactoryManager)
        {
            // Instantiate the page from the virtual path
            object instance = virtualPathFactoryManager.CreateInstance <object>(virtualPath);

            WebPage page = instance as WebPage;

            // If it's not a page, assume it's a regular handler
            if (page == null)
            {
                return((IHttpHandler)instance);
            }

            // 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;

            // Return a handler over it
            return(new WebPageHttpHandler(page));
        }
Example #3
0
        internal static WebPageBase CreateInstanceFromVirtualPath(string virtualPath, VirtualPathFactoryManager virtualPathFactoryManager)
        {
            // Get the compiled object (through the VPP)
            try {
                WebPageBase webPage = virtualPathFactoryManager.CreateInstance <WebPageBase>(virtualPath);

                // Give it its virtual path
                webPage.VirtualPath = virtualPath;

                return(webPage);
            }
            catch (HttpException e) {
                Util.ThrowIfUnsupportedExtension(virtualPath, e);
                throw;
            }
        }
Example #4
0
 internal static WebPageBase CreateInstanceFromVirtualPath(string virtualPath, Func <string, Type, object> createInstanceMethod)
 {
     return(CreateInstanceFromVirtualPath(virtualPath, VirtualPathFactoryManager.CreateFromLambda(virtualPath, createInstanceMethod)));
 }
Example #5
0
        internal static WebPageMatch MatchRequest(string pathValue, IEnumerable <string> supportedExtensions, VirtualPathFactoryManager virtualPathFactoryManager)
        {
            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, virtualPathFactoryManager))
                {
                    // 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, virtualPathFactoryManager);
                    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);
                    }
                }
            }

            // 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, virtualPathFactoryManager);

            if (defaultMatch != null)
            {
                return(new WebPageMatch(defaultMatch, String.Empty));
            }

            string indexMatch = GetRouteLevelMatch(currentLevelIndex, supportedExtensions, virtualPathFactoryManager);

            if (indexMatch != null)
            {
                return(new WebPageMatch(indexMatch, String.Empty));
            }

            return(null);
        }
Example #6
0
        private static string GetRouteLevelMatch(string pathValue, IEnumerable <string> supportedExtensions, VirtualPathFactoryManager virtualPathFactoryManager)
        {
            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;
                }
                if (FileExists(virtualPath, virtualPathFactoryManager))
                {
                    // If there's an exact match on disk, return it unless it starts with an underscore
                    if (Path.GetFileName(pathValue).StartsWith("_", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new HttpException(WebPageResources.WebPageRoute_UnderscoreBlocked);
                    }

                    return(virtualPath);
                }
            }

            return(null);
        }
Example #7
0
        private static bool FileExists(string virtualPath, VirtualPathFactoryManager virtualPathFactoryManager)
        {
            var path = "~/" + virtualPath;

            return(virtualPathFactoryManager.PageExists(path));
        }