diff --git a/samples/MvcSandbox/Pages/_PageImports.cshtml b/samples/MvcSandbox/Pages/_PageImports.cshtml deleted file mode 100644 index 61d5243f3a..0000000000 --- a/samples/MvcSandbox/Pages/_PageImports.cshtml +++ /dev/null @@ -1 +0,0 @@ -@using MvcSandbox \ No newline at end of file diff --git a/samples/MvcSandbox/Pages/_ViewImports.cshtml b/samples/MvcSandbox/Pages/_ViewImports.cshtml new file mode 100644 index 0000000000..96ba4523d1 --- /dev/null +++ b/samples/MvcSandbox/Pages/_ViewImports.cshtml @@ -0,0 +1,2 @@ +@using MvcSandbox +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs index e1039f376a..47b75de1f8 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs @@ -157,24 +157,19 @@ namespace Microsoft.Extensions.DependencyInjection // creating the singleton RazorViewEngine instance. services.TryAddTransient(); + + // + // Razor compilation infrastructure + // services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(s => { return RazorEngine.Create(b => { - InjectDirective.Register(b); - ModelDirective.Register(b); - PageDirective.Register(b); - - b.AddTargetExtension(new InjectDirectiveTargetExtension()); - - b.Features.Add(new ModelExpressionPass()); - b.Features.Add(new PagesPropertyInjectionPass()); - b.Features.Add(new ViewComponentTagHelperPass()); - b.Features.Add(new RazorPageDocumentClassifierPass()); - b.Features.Add(new MvcViewDocumentClassifierPass()); - b.Features.Add(new DefaultInstrumentationPass()); + RazorExtensions.Register(b); b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperFeature()); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorPageFactoryProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorPageFactoryProvider.cs index 150c07393d..5729d0ddd3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorPageFactoryProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorPageFactoryProvider.cs @@ -4,9 +4,6 @@ using System; using System.Linq.Expressions; using System.Reflection; -using Microsoft.AspNetCore.Mvc.Razor.Compilation; -using Microsoft.AspNetCore.Mvc.Razor.Extensions; -using Microsoft.AspNetCore.Razor.Evolution; namespace Microsoft.AspNetCore.Mvc.Razor.Internal { @@ -16,26 +13,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal /// public class DefaultRazorPageFactoryProvider : IRazorPageFactoryProvider { - private const string ViewImportsFileName = "_ViewImports.cshtml"; - private readonly RazorCompiler _razorCompiler; + private readonly RazorCompiler _compiler; /// /// Initializes a new instance of . /// - /// The . - /// The . - /// The . - /// The . - public DefaultRazorPageFactoryProvider( - RazorEngine razorEngine, - RazorProject razorProject, - ICompilationService compilationService, - ICompilerCacheProvider compilerCacheProvider) + /// The . + public DefaultRazorPageFactoryProvider(RazorCompiler compiler) { - var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); - templateEngine.Options.ImportsFileName = ViewImportsFileName; - - _razorCompiler = new RazorCompiler(compilationService, compilerCacheProvider, templateEngine); + _compiler = compiler; } /// @@ -52,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal relativePath = relativePath.Substring(1); } - var result = _razorCompiler.Compile(relativePath); + var result = _compiler.Compile(relativePath); if (result.Success) { var compiledType = result.CompiledType; diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompiler.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompiler.cs index 79a6cf14da..51c30b3607 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompiler.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompiler.cs @@ -16,14 +16,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal { private readonly ICompilationService _compilationService; private readonly ICompilerCacheProvider _compilerCacheProvider; - private readonly MvcRazorTemplateEngine _templateEngine; + private readonly RazorTemplateEngine _templateEngine; private readonly Func _getCacheContext; private readonly Func _getCompilationResultDelegate; public RazorCompiler( ICompilationService compilationService, ICompilerCacheProvider compilerCacheProvider, - MvcRazorTemplateEngine templateEngine) + RazorTemplateEngine templateEngine) { _compilationService = compilationService; _compilerCacheProvider = compilerCacheProvider; diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs index 3066d03b26..f4074b21e3 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure { if (item.FileName.StartsWith("_")) { - // Pages like _PageImports should not be routable. + // Files like _ViewImports.cshtml should not be routable. continue; } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs index e44b5a61d8..6da72a0661 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs @@ -2,36 +2,25 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Reflection; -using Microsoft.AspNetCore.Mvc.Razor.Compilation; -using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Mvc.Razor.Internal; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; -using Microsoft.AspNetCore.Razor.Evolution; namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal { public class DefaultPageLoader : IPageLoader { - private const string PageImportsFileName = "_PageImports.cshtml"; private const string ModelPropertyName = "Model"; + + private readonly RazorCompiler _compiler; - private readonly MvcRazorTemplateEngine _templateEngine; - private readonly RazorCompiler _razorCompiler; - - public DefaultPageLoader( - RazorEngine razorEngine, - RazorProject razorProject, - ICompilationService compilationService, - ICompilerCacheProvider compilerCacheProvider) + public DefaultPageLoader(RazorCompiler compiler) { - _templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); - _templateEngine.Options.ImportsFileName = PageImportsFileName; - _razorCompiler = new RazorCompiler(compilationService, compilerCacheProvider, _templateEngine); + _compiler = compiler; } public CompiledPageActionDescriptor Load(PageActionDescriptor actionDescriptor) { - var compilationResult = _razorCompiler.Compile(actionDescriptor.RelativePath); + var compilationResult = _compiler.Compile(actionDescriptor.RelativePath); var compiledTypeInfo = compilationResult.CompiledType.GetTypeInfo(); // If a model type wasn't set in code then the model property's type will be the same // as the compiled type. diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorPageFactoryProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorPageFactoryProviderTest.cs index c31d8562f1..d1cb26e143 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorPageFactoryProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorPageFactoryProviderTest.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Razor.Compilation; +using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Razor.Evolution; using Microsoft.Extensions.Primitives; using Moq; @@ -27,15 +28,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal compilerCache .Setup(f => f.GetOrAdd(It.IsAny(), It.IsAny>())) .Returns(new CompilerCacheResult(path, expirationTokens)); - var compilerCacheProvider = new Mock(); - compilerCacheProvider - .SetupGet(c => c.Cache) - .Returns(compilerCache.Object); - var factoryProvider = new DefaultRazorPageFactoryProvider( - RazorEngine.Create(), - new DefaultRazorProject(new TestFileProvider()), - Mock.Of(), - compilerCacheProvider.Object); + + var factoryProvider = new DefaultRazorPageFactoryProvider(CreateCompiler(compilerCache.Object)); // Act var result = factoryProvider.CreateFactory(path); @@ -59,15 +53,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal compilerCache .Setup(f => f.GetOrAdd(It.IsAny(), It.IsAny>())) .Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), expirationTokens)); - var compilerCacheProvider = new Mock(); - compilerCacheProvider - .SetupGet(c => c.Cache) - .Returns(compilerCache.Object); - var factoryProvider = new DefaultRazorPageFactoryProvider( - RazorEngine.Create(), - new DefaultRazorProject(new TestFileProvider()), - Mock.Of(), - compilerCacheProvider.Object); + + var factoryProvider = new DefaultRazorPageFactoryProvider(CreateCompiler(compilerCache.Object)); // Act var result = factoryProvider.CreateFactory(relativePath); @@ -86,15 +73,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal compilerCache .Setup(f => f.GetOrAdd(It.IsAny(), It.IsAny>())) .Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), new IChangeToken[0])); - var compilerCacheProvider = new Mock(); - compilerCacheProvider - .SetupGet(c => c.Cache) - .Returns(compilerCache.Object); - var factoryProvider = new DefaultRazorPageFactoryProvider( - RazorEngine.Create(), - new DefaultRazorProject(new TestFileProvider()), - Mock.Of(), - compilerCacheProvider.Object); + + var factoryProvider = new DefaultRazorPageFactoryProvider(CreateCompiler(compilerCache.Object)); // Act var result = factoryProvider.CreateFactory(relativePath); @@ -105,6 +85,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal Assert.Equal("/file-exists", actual.Path); } + private RazorCompiler CreateCompiler(ICompilerCache cache) + { + var compilerCacheProvider = new Mock(); + compilerCacheProvider + .SetupGet(c => c.Cache) + .Returns(cache); + + return new RazorCompiler( + Mock.Of(), + compilerCacheProvider.Object, + new MvcRazorTemplateEngine(RazorEngine.Create(), new DefaultRazorProject(new TestFileProvider()))); + } + private class TestRazorPage : RazorPage { public override Task ExecuteAsync() diff --git a/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/_PageImports.cshtml b/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/_ViewImports.cshtml similarity index 100% rename from test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/_PageImports.cshtml rename to test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/_ViewImports.cshtml diff --git a/test/WebSites/RazorPagesWebSite/Pages/WithPageImport/_PageImports.cshtml b/test/WebSites/RazorPagesWebSite/Pages/WithPageImport/_ViewImports.cshtml similarity index 100% rename from test/WebSites/RazorPagesWebSite/Pages/WithPageImport/_PageImports.cshtml rename to test/WebSites/RazorPagesWebSite/Pages/WithPageImport/_ViewImports.cshtml