Updated Razor Pages areas for compatibility switches

This commit is contained in:
Ryan Nowak 2018-01-01 19:13:43 -08:00
parent 64a11e2093
commit 49bdcfb150
12 changed files with 117 additions and 20 deletions

View File

@ -51,9 +51,8 @@ namespace Microsoft.AspNetCore.Mvc
/// <remarks>
/// ASP.NET Core MVC 2.1 introduces compatibility switches for the following:
/// <list type="bullet">
/// <item>
/// <description><see cref="MvcOptions.SuppressBindingUndefinedValueToEnumType"/></description>
/// </item>
/// <item><description><see cref="MvcOptions.SuppressBindingUndefinedValueToEnumType"/></description></item>
/// <item><description><c>RazorPagesOptions.AllowAreas</c></description></item>
/// </list>
/// </remarks>
Version_2_1,

View File

@ -81,6 +81,8 @@ namespace Microsoft.Extensions.DependencyInjection
// Options
services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<RazorViewEngineOptions>, RazorPagesRazorViewEngineOptionsSetup>());
services.TryAddEnumerable(
ServiceDescriptor.Transient<IPostConfigureOptions<RazorPagesOptions>, RazorPagesOptionsConfigureCompatibilityOptions>());
// Action description and invocation
services.TryAddEnumerable(

View File

@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{
model = GetPageRouteModel(rootDirectory, viewDescriptor);
}
else if (_pagesOptions.EnableAreas && viewDescriptor.RelativePath.StartsWith(areaRootDirectory, StringComparison.OrdinalIgnoreCase))
else if (_pagesOptions.AllowAreas && viewDescriptor.RelativePath.StartsWith(areaRootDirectory, StringComparison.OrdinalIgnoreCase))
{
model = GetAreaPageRouteModel(areaRootDirectory, viewDescriptor);
}

View File

@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var additionalImportFilePaths = templateEngine.GetImportItems(importFileAtPagesRoot)
.Select(item => item.FilePath);
if (razorPagesOptions.Value.EnableAreas)
if (razorPagesOptions.Value.AllowAreas)
{
var areaRootDirectory = razorPagesOptions.Value.AreaRootDirectory;
Debug.Assert(!string.IsNullOrEmpty(areaRootDirectory));

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{
AddPageModels(context);
if (_pagesOptions.EnableAreas)
if (_pagesOptions.AllowAreas)
{
AddAreaPageModels(context);
}

View File

@ -2,18 +2,34 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace Microsoft.AspNetCore.Mvc.RazorPages
{
/// <summary>
/// Provides configuration for RazorPages.
/// </summary>
public class RazorPagesOptions
public class RazorPagesOptions : IEnumerable<ICompatibilitySwitch>
{
private readonly CompatibilitySwitch<bool> _allowAreas;
private readonly ICompatibilitySwitch[] _switches;
private string _root = "/Pages";
private string _areasRoot = "/Areas";
public RazorPagesOptions()
{
_allowAreas = new CompatibilitySwitch<bool>(nameof(AllowAreas));
_switches = new ICompatibilitySwitch[]
{
_allowAreas,
};
}
/// <summary>
/// Gets a collection of <see cref="IPageConvention"/> instances that are applied during
/// route and page model construction.
@ -45,20 +61,43 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
/// <summary>
/// Gets or sets a value that determines if areas are enabled for Razor Pages.
/// Defaults to <c>true</c>.
/// Defaults to <c>false</c>.
/// </summary>
/// <remarks>
/// <para>
/// When enabled, any Razor Page under the directory structure <c>/{AreaRootDirectory}/AreaName/{RootDirectory}/</c>
/// will be associated with an area with the name <c>AreaName</c>.
/// <seealso cref="AreaRootDirectory"/>
/// <seealso cref="RootDirectory"/>
/// </para>
/// </summary>
public bool EnableAreas { get; set; }
/// <para>
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See <see cref="CompatibilityVersion"/> for
/// guidance and examples of setting the application's compatibility version.
/// </para>
/// <para>
/// Configuring the desired of the value compatibility switch by calling this property's setter will take precedence
/// over the value implied by the application's <see cref="CompatibilityVersion"/>.
/// </para>
/// <para>
/// If the application's compatibility version is set to <see cref="CompatibilityVersion.Version_2_0"/> then
/// this setting will have value <c>false</c> if not explicitly configured.
/// </para>
/// <para>
/// If the application's compatibility version is set to <see cref="CompatibilityVersion.Version_2_1"/> or
/// higher then this setting will have value <c>true</c> if not explicitly configured.
/// </para>
/// </remarks>
public bool AllowAreas
{
get => _allowAreas.Value;
set => _allowAreas.Value = value;
}
/// <summary>
/// Application relative path used as the root of discovery for Razor Page files associated with areas.
/// Defaults to the <c>/Areas</c> directory under application root.
/// <seealso cref="EnableAreas" />
/// <seealso cref="AllowAreas" />
/// </summary>
public string AreaRootDirectory
{
@ -78,5 +117,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
_areasRoot = value;
}
}
IEnumerator<ICompatibilitySwitch> IEnumerable<ICompatibilitySwitch>.GetEnumerator()
{
return ((IEnumerable<ICompatibilitySwitch>)_switches).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => _switches.GetEnumerator();
}
}

View File

@ -0,0 +1,35 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Mvc.RazorPages
{
internal class RazorPagesOptionsConfigureCompatibilityOptions : ConfigureCompatibilityOptions<RazorPagesOptions>
{
public RazorPagesOptionsConfigureCompatibilityOptions(
ILoggerFactory loggerFactory,
IOptions<MvcCompatibilityOptions> compatibilityOptions)
: base(loggerFactory, compatibilityOptions)
{
}
protected override IReadOnlyDictionary<string, object> DefaultValues
{
get
{
var values = new Dictionary<string, object>();
if (Version >= CompatibilityVersion.Version_2_1)
{
values[nameof(RazorPagesOptions.AllowAreas)] = true;
}
return values;
}
}
}
}

View File

@ -73,7 +73,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
};
var options = new RazorPagesOptions
{
EnableAreas = true,
AllowAreas = true,
AreaRootDirectory = "/Features",
RootDirectory = "/Files",
};
@ -151,7 +151,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
GetDescriptor("/Pages/About.cshtml"),
GetDescriptor("/Areas/Accounts/Pages/Home.cshtml"),
};
var options = new RazorPagesOptions { EnableAreas = false };
var options = new RazorPagesOptions { AllowAreas = false };
var provider = new TestCompiledPageRouteModelProvider(descriptors, options);
var context = new PageRouteModelProviderContext();

View File

@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(),
new FileProviderRazorProject(accessor));
var options = Options.Create(new RazorPagesOptions { EnableAreas = true });
var options = Options.Create(new RazorPagesOptions { AllowAreas = true });
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
// Act
@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
new FileProviderRazorProject(accessor));
var options = Options.Create(new RazorPagesOptions
{
EnableAreas = true,
AllowAreas = true,
AreaRootDirectory = rootDirectory,
});
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
@ -150,7 +150,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = "/dir1/dir2";
options.Value.AreaRootDirectory = "/dir3/dir4";
options.Value.EnableAreas = true;
options.Value.AllowAreas = true;
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
@ -175,7 +175,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
RazorEngine.Create(),
new FileProviderRazorProject(accessor));
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions { EnableAreas = false });
var options = Options.Create(new RazorPagesOptions { AllowAreas = false });
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);

View File

@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var project = new TestRazorProject(fileProvider);
var optionsManager = Options.Create(new RazorPagesOptions { EnableAreas = true });
var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = true });
var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
var context = new PageRouteModelProviderContext();
@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var project = new TestRazorProject(fileProvider);
var optionsManager = Options.Create(new RazorPagesOptions { EnableAreas = false });
var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = false });
var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
var context = new PageRouteModelProviderContext();

View File

@ -23,6 +23,7 @@ using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.AspNetCore.Mvc.RazorPages.Internal;
using Microsoft.AspNetCore.Mvc.TagHelpers;
@ -371,6 +372,20 @@ namespace Microsoft.AspNetCore.Mvc
typeof(RazorPagesRazorViewEngineOptionsSetup),
}
},
{
typeof(IPostConfigureOptions<MvcOptions>),
new[]
{
typeof(MvcOptions).Assembly.GetType("Microsoft.AspNetCore.Mvc.Infrastructure.MvcOptionsConfigureCompatibilityOptions", throwOnError: true),
}
},
{
typeof(IPostConfigureOptions<RazorPagesOptions>),
new[]
{
typeof(RazorPagesOptions).Assembly.GetType("Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptionsConfigureCompatibilityOptions", throwOnError: true),
}
},
{
typeof(IActionConstraintProvider),
new Type[]

View File

@ -17,7 +17,7 @@ namespace RazorPagesWebSite
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{
options.EnableAreas = true;
options.AllowAreas = true;
options.Conventions.AuthorizePage("/Conventions/Auth");
options.Conventions.AuthorizeFolder("/Conventions/AuthFolder");
});