コード例 #1
0
        public KeyedStaticFileProvider(IHttpContextAccessor contextAccessor,
                                       IOptions <RazorMultiViewEngineOptions> viewOptions,
                                       IWebHostEnvironment environment)
        {
            this.contextAccessor  = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
            this.originalProvider = environment?.WebRootFileProvider ?? throw new ArgumentNullException(nameof(originalProvider));
            this.viewOptions      = viewOptions?.Value ?? throw new ArgumentNullException(nameof(viewOptions));

            this.embeddedProviders = new Dictionary <string, IFileProvider>();

            foreach (var library in this.viewOptions.ViewLibraries)
            {
                if (!string.IsNullOrEmpty(library.StaticFilePath))
                {
                    var assembly = AppDomain.CurrentDomain.EnsureAssembly(library.AssemblyName);
                    if (string.IsNullOrEmpty(library.PathRelativeToContentRoot))
                    {
                        embeddedProviders.Add(library.AssemblyName, new ManifestEmbeddedFileProvider(assembly, library.StaticFilePath));
                    }
                    else
                    {
                        var path = Path.GetFullPath(Path.Combine(environment.ContentRootPath, library.PathRelativeToContentRoot, library.StaticFilePath));
                        embeddedProviders.Add(library.AssemblyName, new PhysicalFileProvider(path));
                    }
                }
            }
        }
コード例 #2
0
        public RazorMultiViewEngine(IHttpContextAccessor contextAccessor,
                                    IRazorPageFactoryProvider pageFactory,
                                    IRazorPageActivator pageActivator,
                                    HtmlEncoder htmlEncoder,
                                    IOptions <RazorMultiViewEngineOptions> optionsAccessor,
                                    DiagnosticListener diagnosticListener)
        {
            this.pageFactory        = pageFactory ?? throw new ArgumentNullException(nameof(pageFactory));
            this.pageActivator      = pageActivator ?? throw new ArgumentNullException(nameof(pageActivator));
            this.htmlEncoder        = htmlEncoder ?? throw new ArgumentNullException(nameof(htmlEncoder));
            this.options            = optionsAccessor?.Value ?? throw new ArgumentNullException(nameof(optionsAccessor));
            this.diagnosticListener = diagnosticListener ?? throw new ArgumentNullException(nameof(diagnosticListener));

            if (contextAccessor is null)
            {
                throw new ArgumentNullException(nameof(contextAccessor));
            }

            this.ViewLookupCache = new MultiTenantMemoryCache(contextAccessor, options.HttpContextItemsKey);
        }
コード例 #3
0
        public RazorMultiViewCompilerProvider(ApplicationPartManager applicationPartManager,
                                              IHttpContextAccessor contextAccessor,
                                              IOptions <RazorMultiViewEngineOptions> optionsAccessor)
        {
            if (applicationPartManager is null)
            {
                throw new ArgumentNullException(nameof(applicationPartManager));
            }

            this.contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
            this.options         = optionsAccessor?.Value ?? throw new ArgumentNullException(nameof(optionsAccessor));

            var feature = new ViewsFeature();

            applicationPartManager.PopulateFeature(feature);

            var defaultViews = new List <CompiledViewDescriptor>();

            foreach (var descriptor in feature.ViewDescriptors.Where(f => f.Item.Type.Assembly.GetName().Name.Equals(options.DefaultViewLibrary?.AssemblyName) ||
                                                                     f.Item.Type.Assembly.GetName().Name.Equals(options.DefaultViewLibrary?.AssemblyName + ".Views", StringComparison.Ordinal)))
            {
                if (!defaultViews.Exists(v => v.RelativePath.Equals(descriptor.RelativePath, StringComparison.OrdinalIgnoreCase)))
                {
                    defaultViews.Add(descriptor);
                }
            }

            compilers.Add("default", new RazorMultiViewCompiler(defaultViews));

            // A cache list of libraries and their compiled views
            var libraryViewList = new Dictionary <string, List <CompiledViewDescriptor> >();

            if (options.ViewLibraryConfig is object)
            {
                foreach (var option in options.ViewLibraryConfig)
                {
                    if (compilers.ContainsKey(option.Key))
                    {
                        continue;
                    }

                    // A list of descriptors for this option
                    var viewDescriptors = new List <CompiledViewDescriptor>();

                    // Loop the requested libraries
                    // TODO: Find a better way of getting the related View assembly
                    foreach (var library in option.Value)
                    {
                        if (!libraryViewList.TryGetValue(library, out var liblist))
                        {
                            liblist = feature.ViewDescriptors.Where(d => d.Item.Type.Assembly.GetName().Name.Equals($"{library}") || d.Item.Type.Assembly.GetName().Name.Equals($"{library}.Views")).ToList();
                        }

                        foreach (var descriptor in liblist)
                        {
                            if (viewDescriptors.Exists(v => v.RelativePath.Equals(descriptor.RelativePath, StringComparison.OrdinalIgnoreCase)))
                            {
                                continue;
                            }
                            viewDescriptors.Add(descriptor);
                        }
                    }

                    // Add any missing views from the default library
                    foreach (var descriptor in defaultViews)
                    {
                        if (viewDescriptors.Exists(v => v.RelativePath.Equals(descriptor.RelativePath, StringComparison.OrdinalIgnoreCase)))
                        {
                            continue;
                        }
                        viewDescriptors.Add(descriptor);
                    }
                    compilers.Add(option.Key, new RazorMultiViewCompiler(viewDescriptors));
                }
            }
        }