Пример #1
0
        private RazorEngine GetRazorEngine()
        {
            var razorEngine = RazorEngine.Create(builder =>
            {
                InjectDirective.Register(builder);
                ModelDirective.Register(builder);
                NamespaceDirective.Register(builder);
                PageDirective.Register(builder);
                FunctionsDirective.Register(builder);
                InheritsDirective.Register(builder);
                SectionDirective.Register(builder);

                ////builder.AddTargetExtension(new TemplateTargetExtension()
                ////{
                ////    TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
                ////});

                ////builder.Features.Add(new SuppressChecksumOptionsFeature());
                builder.Features.Add(new ModelExpressionPass());
                builder.Features.Add(new PagesPropertyInjectionPass());
                builder.Features.Add(new ViewComponentTagHelperPass());
                builder.Features.Add(new RazorPageDocumentClassifierPass());
                builder.Features.Add(new MvcViewDocumentClassifierPass2(RuntimeTemplatesNamespace, BaseViewName));
                builder.Features.Add(new AssemblyAttributeInjectionPass2());

                if (!builder.DesignTime)
                {
                    //builder.Features.Add(new InstrumentationPass());
                }
            });

            return(razorEngine);
        }
Пример #2
0
        public static RazorEngine CreateInstance(string baseType)
        {
            var configuration      = RazorConfiguration.Default;
            var razorProjectEngine = RazorProjectEngine.Create(configuration,
                                                               new NullRazorProjectFileSystem(), builder =>
            {
                TkInjectDirective.Register(builder);
                TkModelDirective.Register(builder);

                if (!RazorLanguageVersion.TryParse("3.0", out var razorLanguageVersion) ||
                    configuration.LanguageVersion.CompareTo(razorLanguageVersion) < 0)
                {
                    NamespaceDirective.Register(builder);
                    FunctionsDirective.Register(builder);
                    InheritsDirective.Register(builder);
                }
                SectionDirective.Register(builder);

                builder.Features.Add(new ModelExpressionPass());
                builder.Features.Add(new RazorTemplateDocumentClassifierPass(baseType));
                builder.Features.Add(new RazorAssemblyAttributeInjectionPass());
#if NETSTANDARD2_0
                builder.Features.Add(new InstrumentationPass());
#endif
                builder.AddTargetExtension(new TemplateTargetExtension()
                {
                    TemplateTypeName = "global::YJC.Toolkit.Razor.TkRazorHelperResult",
                });

                OverrideRuntimeNodeWriterTemplateTypeNamePhase.Register(builder);
            });

            return(razorProjectEngine.Engine);
        }
Пример #3
0
        public static RazorProjectEngine Create(
            RazorConfiguration configuration,
            RazorProjectFileSystem fileSystem,
            Action <RazorProjectEngineBuilder> configure)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var builder = new DefaultRazorProjectEngineBuilder(configuration, fileSystem);

            // The intialization order is somewhat important.
            //
            // Defaults -> Extensions -> Additional customization
            //
            // This allows extensions to rely on default features, and customizations to override choices made by
            // extensions.
            AddDefaultPhases(builder.Phases);
            AddDefaultFeatures(builder.Features);

            if (configuration.LanguageVersion.CompareTo(RazorLanguageVersion.Version_5_0) >= 0)
            {
                builder.Features.Add(new ViewCssScopePass());
            }

            if (configuration.LanguageVersion.CompareTo(RazorLanguageVersion.Version_3_0) >= 0)
            {
                FunctionsDirective.Register(builder);
                ImplementsDirective.Register(builder);
                InheritsDirective.Register(builder);
                NamespaceDirective.Register(builder);
                AttributeDirective.Register(builder);

                AddComponentFeatures(builder, configuration.LanguageVersion);
            }

            LoadExtensions(builder, configuration.Extensions);

            configure?.Invoke(builder);

            return(builder.Build());
        }
Пример #4
0
    public static void Register(RazorProjectEngineBuilder builder)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        FunctionsDirective.Register(builder);
        InjectDirective.Register(builder);
        ModelDirective.Register(builder);
        NamespaceDirective.Register(builder);
        PageDirective.Register(builder);

        InheritsDirective.Register(builder);
        SectionDirective.Register(builder);

        builder.Features.Add(new DefaultTagHelperDescriptorProvider());
        builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());

        builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
        builder.AddTargetExtension(new TemplateTargetExtension()
        {
            TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
        });

        builder.Features.Add(new ModelExpressionPass());
        builder.Features.Add(new PagesPropertyInjectionPass());
        builder.Features.Add(new ViewComponentTagHelperPass());
        builder.Features.Add(new RazorPageDocumentClassifierPass());
        builder.Features.Add(new MvcViewDocumentClassifierPass());
        builder.Features.Add(new AssemblyAttributeInjectionPass());
        builder.Features.Add(new InstrumentationPass());

        builder.Features.Add(new MvcImportProjectFeature());

        // The default C# language version for what this Razor configuration supports.
        builder.SetCSharpLanguageVersion(LanguageVersion.CSharp7_3);
    }