From ccdaa5a7291683c4b8fd26f898848a40cfe0698e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 21 Apr 2017 18:43:56 -0700 Subject: [PATCH] Make /Pages the default root for pages. Fixes #6184 --- .../Pages/{Index.cshtml => PagesHome.cshtml} | 0 samples/MvcSandbox/Startup.cs | 2 +- .../MvcRazorPagesMvcBuilderExtensions.cs | 44 +++++++++++++++++++ .../MvcRazorPagesMvcCoreBuilderExtensions.cs | 22 ++++++++++ .../Internal/RazorPagesOptionsSetup.cs | 2 + .../MvcSandboxTest.cs | 2 +- .../Internal/RazorPagesOptionsSetupTest.cs | 14 ++++++ test/WebSites/RazorPagesWebSite/Startup.cs | 3 +- .../RazorPagesWebSite/StartupWithBasePath.cs | 1 - 9 files changed, 86 insertions(+), 4 deletions(-) rename samples/MvcSandbox/Pages/{Index.cshtml => PagesHome.cshtml} (100%) diff --git a/samples/MvcSandbox/Pages/Index.cshtml b/samples/MvcSandbox/Pages/PagesHome.cshtml similarity index 100% rename from samples/MvcSandbox/Pages/Index.cshtml rename to samples/MvcSandbox/Pages/PagesHome.cshtml diff --git a/samples/MvcSandbox/Startup.cs b/samples/MvcSandbox/Startup.cs index 748aa8d517..1984d71708 100644 --- a/samples/MvcSandbox/Startup.cs +++ b/samples/MvcSandbox/Startup.cs @@ -16,7 +16,7 @@ namespace MvcSandbox // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc().AddCookieTempDataProvider(); + services.AddMvc(); services.Insert(0, ServiceDescriptor.Singleton( typeof(IConfigureOptions), diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcBuilderExtensions.cs index 353eaab441..0655c3bb7d 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcBuilderExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -35,5 +36,48 @@ namespace Microsoft.Extensions.DependencyInjection builder.Services.Configure(setupAction); return builder; } + + /// + /// Configures Razor Pages to use the specified . + /// + /// The . + /// The application relative path to use as the root directory. + /// The . + public static IMvcBuilder WithRazorPagesRoot(this IMvcBuilder builder, string rootDirectory) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrEmpty(rootDirectory)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(rootDirectory)); + } + + if (rootDirectory[0] != '/') + { + throw new ArgumentException(Resources.PathMustBeAnAppRelativePath, nameof(rootDirectory)); + } + + builder.Services.Configure(options => options.RootDirectory = rootDirectory); + return builder; + } + + /// + /// Configures Razor Pages to be rooted at the content root (). + /// + /// The . + /// The . + public static IMvcBuilder WithRazorPagesAtContentRoot(this IMvcBuilder builder) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + builder.Services.Configure(options => options.RootDirectory = "/"); + return builder; + } } } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcCoreBuilderExtensions.cs index bdd060d539..87e2a239c4 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/DependencyInjection/MvcRazorPagesMvcCoreBuilderExtensions.cs @@ -58,6 +58,28 @@ namespace Microsoft.Extensions.DependencyInjection return builder; } + /// + /// Configures Razor Pages to use the specified . + /// + /// The . + /// The application relative path to use as the root directory. + /// + public static IMvcCoreBuilder WithRazorPagesRoot(this IMvcCoreBuilder builder, string rootDirectory) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrEmpty(rootDirectory)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(rootDirectory)); + } + + builder.Services.Configure(options => options.RootDirectory = rootDirectory); + return builder; + } + private static void AddFeatureProviders(IMvcCoreBuilder builder) { if (!builder.PartManager.FeatureProviders.OfType().Any()) diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/RazorPagesOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/RazorPagesOptionsSetup.cs index 929e2bc98a..dde0e9bf01 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/RazorPagesOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/RazorPagesOptionsSetup.cs @@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Always require an antiforgery token on post options.ConfigureFilter(new AutoValidateAntiforgeryTokenAttribute()); + + options.RootDirectory = "/Pages"; } } } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs index e775067153..7387eb4200 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs @@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public async Task RazorPages_ReturnSuccess() { // Arrange & Act - var response = await Client.GetStringAsync("http://localhost/Pages/"); + var response = await Client.GetStringAsync("http://localhost/PagesHome"); // Assert Assert.Contains("This file should give you a quick view of a Mvc Razor Page in action.", response); diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/RazorPagesOptionsSetupTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/RazorPagesOptionsSetupTest.cs index 95a4cf6aa2..80980b3c14 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/RazorPagesOptionsSetupTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/RazorPagesOptionsSetupTest.cs @@ -29,5 +29,19 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal filter => Assert.IsType(filter), filter => Assert.IsType(filter)); } + + [Fact] + public void Configure_SetsRazorPagesRoot() + { + // Arrange + var options = new RazorPagesOptions(); + var setup = new RazorPagesOptionsSetup(); + + // Act + setup.Configure(options); + + // Assert + Assert.Equal("/Pages", options.RootDirectory); + } } } diff --git a/test/WebSites/RazorPagesWebSite/Startup.cs b/test/WebSites/RazorPagesWebSite/Startup.cs index 782112df88..8a12746fcc 100644 --- a/test/WebSites/RazorPagesWebSite/Startup.cs +++ b/test/WebSites/RazorPagesWebSite/Startup.cs @@ -19,7 +19,8 @@ namespace RazorPagesWebSite options.AuthorizePage("/HelloWorldWithAuth"); options.AuthorizeFolder("/Pages/Admin"); options.AllowAnonymousToPage("/Pages/Admin/Login"); - }); + }) + .WithRazorPagesAtContentRoot(); } public void Configure(IApplicationBuilder app) diff --git a/test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs b/test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs index 4ad200c6be..20af9f043c 100644 --- a/test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs +++ b/test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs @@ -16,7 +16,6 @@ namespace RazorPagesWebSite .AddCookieTempDataProvider() .AddRazorPagesOptions(options => { - options.RootDirectory = "/Pages"; options.AuthorizePage("/Conventions/Auth"); options.AuthorizeFolder("/Conventions/AuthFolder"); });