Getting rid of _PageImports

Pages will just use _ViewImports.
This commit is contained in:
Ryan Nowak 2017-04-03 19:44:49 -07:00
parent 9e8d4db7d8
commit c6e4609096
10 changed files with 42 additions and 78 deletions

View File

@ -1 +0,0 @@
@using MvcSandbox

View File

@ -0,0 +1,2 @@
@using MvcSandbox
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

@ -157,24 +157,19 @@ namespace Microsoft.Extensions.DependencyInjection
// creating the singleton RazorViewEngine instance. // creating the singleton RazorViewEngine instance.
services.TryAddTransient<IRazorPageFactoryProvider, DefaultRazorPageFactoryProvider>(); services.TryAddTransient<IRazorPageFactoryProvider, DefaultRazorPageFactoryProvider>();
//
// Razor compilation infrastructure
//
services.TryAddSingleton<RazorProject, DefaultRazorProject>(); services.TryAddSingleton<RazorProject, DefaultRazorProject>();
services.TryAddSingleton<RazorTemplateEngine, MvcRazorTemplateEngine>();
services.TryAddSingleton<RazorCompiler>();
services.TryAddSingleton<RazorEngine>(s => services.TryAddSingleton<RazorEngine>(s =>
{ {
return RazorEngine.Create(b => return RazorEngine.Create(b =>
{ {
InjectDirective.Register(b); RazorExtensions.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());
b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperFeature()); b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperFeature());

View File

@ -4,9 +4,6 @@
using System; using System;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; 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 namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
@ -16,26 +13,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
/// </summary> /// </summary>
public class DefaultRazorPageFactoryProvider : IRazorPageFactoryProvider public class DefaultRazorPageFactoryProvider : IRazorPageFactoryProvider
{ {
private const string ViewImportsFileName = "_ViewImports.cshtml"; private readonly RazorCompiler _compiler;
private readonly RazorCompiler _razorCompiler;
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="DefaultRazorPageFactoryProvider"/>. /// Initializes a new instance of <see cref="DefaultRazorPageFactoryProvider"/>.
/// </summary> /// </summary>
/// <param name="razorEngine">The <see cref="RazorEngine"/>.</param> /// <param name="compiler">The <see cref="RazorCompiler"/>.</param>
/// <param name="razorProject">The <see cref="RazorProject" />.</param> public DefaultRazorPageFactoryProvider(RazorCompiler compiler)
/// <param name="compilationService">The <see cref="ICompilationService"/>.</param>
/// <param name="compilerCacheProvider">The <see cref="ICompilerCacheProvider"/>.</param>
public DefaultRazorPageFactoryProvider(
RazorEngine razorEngine,
RazorProject razorProject,
ICompilationService compilationService,
ICompilerCacheProvider compilerCacheProvider)
{ {
var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); _compiler = compiler;
templateEngine.Options.ImportsFileName = ViewImportsFileName;
_razorCompiler = new RazorCompiler(compilationService, compilerCacheProvider, templateEngine);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -52,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
relativePath = relativePath.Substring(1); relativePath = relativePath.Substring(1);
} }
var result = _razorCompiler.Compile(relativePath); var result = _compiler.Compile(relativePath);
if (result.Success) if (result.Success)
{ {
var compiledType = result.CompiledType; var compiledType = result.CompiledType;

View File

@ -16,14 +16,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
private readonly ICompilationService _compilationService; private readonly ICompilationService _compilationService;
private readonly ICompilerCacheProvider _compilerCacheProvider; private readonly ICompilerCacheProvider _compilerCacheProvider;
private readonly MvcRazorTemplateEngine _templateEngine; private readonly RazorTemplateEngine _templateEngine;
private readonly Func<string, CompilerCacheContext> _getCacheContext; private readonly Func<string, CompilerCacheContext> _getCacheContext;
private readonly Func<CompilerCacheContext, CompilationResult> _getCompilationResultDelegate; private readonly Func<CompilerCacheContext, CompilationResult> _getCompilationResultDelegate;
public RazorCompiler( public RazorCompiler(
ICompilationService compilationService, ICompilationService compilationService,
ICompilerCacheProvider compilerCacheProvider, ICompilerCacheProvider compilerCacheProvider,
MvcRazorTemplateEngine templateEngine) RazorTemplateEngine templateEngine)
{ {
_compilationService = compilationService; _compilationService = compilationService;
_compilerCacheProvider = compilerCacheProvider; _compilerCacheProvider = compilerCacheProvider;

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{ {
if (item.FileName.StartsWith("_")) if (item.FileName.StartsWith("_"))
{ {
// Pages like _PageImports should not be routable. // Files like _ViewImports.cshtml should not be routable.
continue; continue;
} }

View File

@ -2,36 +2,25 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Reflection; 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.Razor.Internal;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.AspNetCore.Razor.Evolution;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{ {
public class DefaultPageLoader : IPageLoader public class DefaultPageLoader : IPageLoader
{ {
private const string PageImportsFileName = "_PageImports.cshtml";
private const string ModelPropertyName = "Model"; private const string ModelPropertyName = "Model";
private readonly RazorCompiler _compiler;
private readonly MvcRazorTemplateEngine _templateEngine; public DefaultPageLoader(RazorCompiler compiler)
private readonly RazorCompiler _razorCompiler;
public DefaultPageLoader(
RazorEngine razorEngine,
RazorProject razorProject,
ICompilationService compilationService,
ICompilerCacheProvider compilerCacheProvider)
{ {
_templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); _compiler = compiler;
_templateEngine.Options.ImportsFileName = PageImportsFileName;
_razorCompiler = new RazorCompiler(compilationService, compilerCacheProvider, _templateEngine);
} }
public CompiledPageActionDescriptor Load(PageActionDescriptor actionDescriptor) public CompiledPageActionDescriptor Load(PageActionDescriptor actionDescriptor)
{ {
var compilationResult = _razorCompiler.Compile(actionDescriptor.RelativePath); var compilationResult = _compiler.Compile(actionDescriptor.RelativePath);
var compiledTypeInfo = compilationResult.CompiledType.GetTypeInfo(); var compiledTypeInfo = compilationResult.CompiledType.GetTypeInfo();
// If a model type wasn't set in code then the model property's type will be the same // If a model type wasn't set in code then the model property's type will be the same
// as the compiled type. // as the compiled type.

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Evolution; using Microsoft.AspNetCore.Razor.Evolution;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Moq; using Moq;
@ -27,15 +28,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
compilerCache compilerCache
.Setup(f => f.GetOrAdd(It.IsAny<string>(), It.IsAny<Func<string, CompilerCacheContext>>())) .Setup(f => f.GetOrAdd(It.IsAny<string>(), It.IsAny<Func<string, CompilerCacheContext>>()))
.Returns(new CompilerCacheResult(path, expirationTokens)); .Returns(new CompilerCacheResult(path, expirationTokens));
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider var factoryProvider = new DefaultRazorPageFactoryProvider(CreateCompiler(compilerCache.Object));
.SetupGet(c => c.Cache)
.Returns(compilerCache.Object);
var factoryProvider = new DefaultRazorPageFactoryProvider(
RazorEngine.Create(),
new DefaultRazorProject(new TestFileProvider()),
Mock.Of<ICompilationService>(),
compilerCacheProvider.Object);
// Act // Act
var result = factoryProvider.CreateFactory(path); var result = factoryProvider.CreateFactory(path);
@ -59,15 +53,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
compilerCache compilerCache
.Setup(f => f.GetOrAdd(It.IsAny<string>(), It.IsAny<Func<string, CompilerCacheContext>>())) .Setup(f => f.GetOrAdd(It.IsAny<string>(), It.IsAny<Func<string, CompilerCacheContext>>()))
.Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), expirationTokens)); .Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), expirationTokens));
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider var factoryProvider = new DefaultRazorPageFactoryProvider(CreateCompiler(compilerCache.Object));
.SetupGet(c => c.Cache)
.Returns(compilerCache.Object);
var factoryProvider = new DefaultRazorPageFactoryProvider(
RazorEngine.Create(),
new DefaultRazorProject(new TestFileProvider()),
Mock.Of<ICompilationService>(),
compilerCacheProvider.Object);
// Act // Act
var result = factoryProvider.CreateFactory(relativePath); var result = factoryProvider.CreateFactory(relativePath);
@ -86,15 +73,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
compilerCache compilerCache
.Setup(f => f.GetOrAdd(It.IsAny<string>(), It.IsAny<Func<string, CompilerCacheContext>>())) .Setup(f => f.GetOrAdd(It.IsAny<string>(), It.IsAny<Func<string, CompilerCacheContext>>()))
.Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), new IChangeToken[0])); .Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), new IChangeToken[0]));
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider var factoryProvider = new DefaultRazorPageFactoryProvider(CreateCompiler(compilerCache.Object));
.SetupGet(c => c.Cache)
.Returns(compilerCache.Object);
var factoryProvider = new DefaultRazorPageFactoryProvider(
RazorEngine.Create(),
new DefaultRazorProject(new TestFileProvider()),
Mock.Of<ICompilationService>(),
compilerCacheProvider.Object);
// Act // Act
var result = factoryProvider.CreateFactory(relativePath); var result = factoryProvider.CreateFactory(relativePath);
@ -105,6 +85,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
Assert.Equal("/file-exists", actual.Path); Assert.Equal("/file-exists", actual.Path);
} }
private RazorCompiler CreateCompiler(ICompilerCache cache)
{
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider
.SetupGet(c => c.Cache)
.Returns(cache);
return new RazorCompiler(
Mock.Of<ICompilationService>(),
compilerCacheProvider.Object,
new MvcRazorTemplateEngine(RazorEngine.Create(), new DefaultRazorProject(new TestFileProvider())));
}
private class TestRazorPage : RazorPage private class TestRazorPage : RazorPage
{ {
public override Task ExecuteAsync() public override Task ExecuteAsync()