public override RazorTemplateEngine Create(string projectPath, Action <IRazorEngineBuilder> configure)
        {
            if (projectPath == null)
            {
                throw new ArgumentNullException(nameof(projectPath));
            }

            // In 15.5 we expect projectPath to be a directory, NOT the path to the csproj.
            var project              = FindProject(projectPath);
            var configuration        = (project?.Configuration as MvcExtensibilityConfiguration) ?? DefaultConfiguration;
            var razorLanguageVersion = configuration.LanguageVersion;
            var razorConfiguration   = new RazorConfiguration(razorLanguageVersion, "unnamed", Array.Empty <RazorExtension>(), designTime: true);

            RazorEngine engine;

            if (razorLanguageVersion.Major == 1)
            {
                engine = RazorEngine.CreateCore(razorConfiguration, b =>
                {
                    configure?.Invoke(b);

                    Mvc1_X.RazorExtensions.Register(b);

                    if (configuration.MvcAssembly.Identity.Version.Minor >= 1)
                    {
                        Mvc1_X.RazorExtensions.RegisterViewComponentTagHelpers(b);
                    }
                });

                var templateEngine = new Mvc1_X.MvcRazorTemplateEngine(engine, RazorProject.Create(projectPath));
                templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
                return(templateEngine);
            }
            else
            {
                engine = RazorEngine.CreateCore(razorConfiguration, b =>
                {
                    configure?.Invoke(b);

                    MvcLatest.RazorExtensions.Register(b);
                });

                var templateEngine = new MvcLatest.MvcRazorTemplateEngine(engine, RazorProject.Create(projectPath));
                templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
                return(templateEngine);
            }
        }
        public void GetDefaultImports_IncludesDefaultTagHelpers()
        {
            // Arrange
            var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
                RazorEngine.Create(),
                new TestRazorProjectFileSystem());

            // Act
            var imports = mvcRazorTemplateEngine.Options.DefaultImports;

            // Assert
            var importContent = GetContent(imports)
                                .Split(new[] { Environment.NewLine }, StringSplitOptions.None)
                                .Where(line => line.StartsWith("@addTagHelper"));

            Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
            Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
            Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
        }
Esempio n. 3
0
        public void CreateCodeDocument_SetsRelativePathOnOutput()
        {
            // Arrange
            var path = "/Views/Home/Index.cshtml";
            var item = new TestRazorProjectItem(path)
            {
                Content = "Hello world",
            };
            var project = new TestRazorProject(new List <RazorProjectItem>()
            {
                item,
            });

            var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
                RazorEngine.Create(),
                project);

            // Act
            var codeDocument = mvcRazorTemplateEngine.CreateCodeDocument(path);

            // Assert
            Assert.Equal(path, codeDocument.GetRelativePath());
        }