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);
            }
        }
示例#2
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());
        }