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.
services.TryAddTransient<IRazorPageFactoryProvider, DefaultRazorPageFactoryProvider>();
//
// Razor compilation infrastructure
//
services.TryAddSingleton<RazorProject, DefaultRazorProject>();
services.TryAddSingleton<RazorTemplateEngine, MvcRazorTemplateEngine>();
services.TryAddSingleton<RazorCompiler>();
services.TryAddSingleton<RazorEngine>(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());

View File

@ -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
/// </summary>
public class DefaultRazorPageFactoryProvider : IRazorPageFactoryProvider
{
private const string ViewImportsFileName = "_ViewImports.cshtml";
private readonly RazorCompiler _razorCompiler;
private readonly RazorCompiler _compiler;
/// <summary>
/// Initializes a new instance of <see cref="DefaultRazorPageFactoryProvider"/>.
/// </summary>
/// <param name="razorEngine">The <see cref="RazorEngine"/>.</param>
/// <param name="razorProject">The <see cref="RazorProject" />.</param>
/// <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)
/// <param name="compiler">The <see cref="RazorCompiler"/>.</param>
public DefaultRazorPageFactoryProvider(RazorCompiler compiler)
{
var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject);
templateEngine.Options.ImportsFileName = ViewImportsFileName;
_razorCompiler = new RazorCompiler(compilationService, compilerCacheProvider, templateEngine);
_compiler = compiler;
}
/// <inheritdoc />
@ -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;

View File

@ -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<string, CompilerCacheContext> _getCacheContext;
private readonly Func<CompilerCacheContext, CompilationResult> _getCompilationResultDelegate;
public RazorCompiler(
ICompilationService compilationService,
ICompilerCacheProvider compilerCacheProvider,
MvcRazorTemplateEngine templateEngine)
RazorTemplateEngine templateEngine)
{
_compilationService = compilationService;
_compilerCacheProvider = compilerCacheProvider;

View File

@ -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;
}

View File

@ -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.

View File

@ -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<string>(), It.IsAny<Func<string, CompilerCacheContext>>()))
.Returns(new CompilerCacheResult(path, expirationTokens));
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider
.SetupGet(c => c.Cache)
.Returns(compilerCache.Object);
var factoryProvider = new DefaultRazorPageFactoryProvider(
RazorEngine.Create(),
new DefaultRazorProject(new TestFileProvider()),
Mock.Of<ICompilationService>(),
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<string>(), It.IsAny<Func<string, CompilerCacheContext>>()))
.Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), expirationTokens));
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider
.SetupGet(c => c.Cache)
.Returns(compilerCache.Object);
var factoryProvider = new DefaultRazorPageFactoryProvider(
RazorEngine.Create(),
new DefaultRazorProject(new TestFileProvider()),
Mock.Of<ICompilationService>(),
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<string>(), It.IsAny<Func<string, CompilerCacheContext>>()))
.Returns(new CompilerCacheResult(relativePath, new CompilationResult(typeof(TestRazorPage)), new IChangeToken[0]));
var compilerCacheProvider = new Mock<ICompilerCacheProvider>();
compilerCacheProvider
.SetupGet(c => c.Cache)
.Returns(compilerCache.Object);
var factoryProvider = new DefaultRazorPageFactoryProvider(
RazorEngine.Create(),
new DefaultRazorProject(new TestFileProvider()),
Mock.Of<ICompilationService>(),
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<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
{
public override Task ExecuteAsync()