Beispiel #1
0
        /// <summary>
        /// Gets the Razor page for an input document stream. This is roughly modeled on
        /// DefaultRazorPageFactory and CompilerCache. Note that we don't actually bother
        /// with caching the page if it's from a live stream.
        /// </summary>
        private IRazorPage GetPageFromStream(
            string relativePath,
            string viewStartLocation,
            string layoutLocation,
            Stream stream,
            IFileProvider rootFileProvider,
            IRazorCompilationService razorCompilationService)
        {
            if (relativePath.StartsWith("~/", StringComparison.Ordinal))
            {
                // For tilde slash paths, drop the leading ~ to make it work with the underlying IFileProvider.
                relativePath = relativePath.Substring(1);
            }

            // Get the file info by combining the stream content with info found at the document's original location (if any)
            IFileInfo        fileInfo         = new StreamFileInfo(rootFileProvider.GetFileInfo(relativePath), stream);
            RelativeFileInfo relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath);

            // Try to get the compilation from the cache, but only if the stream is empty
            // Cache key is relative path if no explicit view start or layout OR either/both of those if specified
            CompilationResult compilationResult = stream.Length == 0
                ? _compilationCache.GetOrAdd(
                viewStartLocation == null
                        ? (layoutLocation ?? relativePath)
                        : (layoutLocation == null ? viewStartLocation : viewStartLocation + layoutLocation),
                _ => GetCompilation(relativeFileInfo, razorCompilationService))
                : GetCompilation(relativeFileInfo, razorCompilationService);

            // Create and return the page
            // We're not actually using the ASP.NET cache, but the CompilerCacheResult ctor contains the logic to create the page factory
            CompilerCacheResult compilerCacheResult = new CompilerCacheResult(relativePath, compilationResult, Array.Empty <IChangeToken>());

            return(compilerCacheResult.PageFactory());
        }
Beispiel #2
0
        /// <summary>
        /// Gets the view for an input document (which is different than the view for a layout, partial, or
        /// other indirect view because it's not necessarily on disk or in the file system).
        /// </summary>
        private IView GetViewFromStream(
            string relativePath,
            Stream stream,
            string viewStartLocation,
            string layoutLocation,
            IRazorViewEngine viewEngine,
            IRazorPageActivator pageActivator,
            HtmlEncoder htmlEncoder,
            IRazorPageFactoryProvider pageFactoryProvider,
            IFileProvider rootFileProvider,
            IRazorCompilationService razorCompilationService)
        {
            IEnumerable <string> viewStartLocations = viewStartLocation != null
                ? new [] { viewStartLocation }
                : ViewHierarchyUtility.GetViewStartLocations(relativePath);
            List <IRazorPage> viewStartPages = viewStartLocations
                                               .Select(pageFactoryProvider.CreateFactory)
                                               .Where(x => x.Success)
                                               .Select(x => x.RazorPageFactory())
                                               .Reverse()
                                               .ToList();
            IRazorPage page = GetPageFromStream(relativePath, viewStartLocation, layoutLocation, stream, rootFileProvider, razorCompilationService);

            if (layoutLocation != null)
            {
                page.Layout = layoutLocation;
            }
            return(new RazorView(viewEngine, pageActivator, viewStartPages, page, htmlEncoder));
        }
Beispiel #3
0
        public HostEnvironment(IFileProvider fileProvider)
        {
            EnvironmentName = "Statiq";

            // This gets used to load dependencies and is passed to Assembly.Load()
            ApplicationName = typeof(HostEnvironment).Assembly.FullName;

            WebRootPath             = ((FileSystemFileProvider)fileProvider).StatiqFileSystem.RootPath.FullPath;
            WebRootFileProvider     = fileProvider;
            ContentRootPath         = WebRootPath;
            ContentRootFileProvider = WebRootFileProvider;
        }
Beispiel #4
0
        /// <summary>
        /// Gets the Razor page for an input document stream. This is roughly modeled on
        /// DefaultRazorPageFactory and CompilerCache. Note that we don't actually bother
        /// with caching the page if it's from a live stream.
        /// </summary>
        private IRazorPage GetPageFromStream(string relativePath, Stream stream,
                                             IFileProvider rootFileProvider, IRazorCompilationService razorCompilationService)
        {
            if (relativePath.StartsWith("~/", StringComparison.Ordinal))
            {
                // For tilde slash paths, drop the leading ~ to make it work with the underlying IFileProvider.
                relativePath = relativePath.Substring(1);
            }

            // Get the file info by combining the stream content with info found at the document's original location (if any)
            IFileInfo        fileInfo         = new StreamFileInfo(rootFileProvider.GetFileInfo(relativePath), stream);
            RelativeFileInfo relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath);

            // Create the compilation
            CompilationResult compilationResult = razorCompilationService.Compile(relativeFileInfo);

            compilationResult.EnsureSuccessful();

            // Create and return the page
            // We're not actually using the cache, but the CompilerCacheResult ctor contains the logic to create the page factory
            CompilerCacheResult compilerCacheResult = new CompilerCacheResult(relativePath, compilationResult, Array.Empty <IChangeToken>());

            return(compilerCacheResult.PageFactory());
        }