Make /Pages the default root for pages.

Fixes #6184
This commit is contained in:
Pranav K 2017-04-21 18:43:56 -07:00
parent 04fd762943
commit ccdaa5a729
9 changed files with 86 additions and 4 deletions

View File

@ -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<AntiforgeryOptions>),

View File

@ -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;
}
/// <summary>
/// Configures Razor Pages to use the specified <paramref name="rootDirectory"/>.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="rootDirectory">The application relative path to use as the root directory.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
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<RazorPagesOptions>(options => options.RootDirectory = rootDirectory);
return builder;
}
/// <summary>
/// Configures Razor Pages to be rooted at the content root (<see cref="IHostingEnvironment.ContentRootPath"/>).
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder WithRazorPagesAtContentRoot(this IMvcBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.Configure<RazorPagesOptions>(options => options.RootDirectory = "/");
return builder;
}
}
}

View File

@ -58,6 +58,28 @@ namespace Microsoft.Extensions.DependencyInjection
return builder;
}
/// <summary>
/// Configures Razor Pages to use the specified <paramref name="rootDirectory"/>.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="rootDirectory">The application relative path to use as the root directory.</param>
/// <returns></returns>
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<RazorPagesOptions>(options => options.RootDirectory = rootDirectory);
return builder;
}
private static void AddFeatureProviders(IMvcCoreBuilder builder)
{
if (!builder.PartManager.FeatureProviders.OfType<CompiledPageFeatureProvider>().Any())

View File

@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Always require an antiforgery token on post
options.ConfigureFilter(new AutoValidateAntiforgeryTokenAttribute());
options.RootDirectory = "/Pages";
}
}
}

View File

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

View File

@ -29,5 +29,19 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
filter => Assert.IsType<PageSaveTempDataPropertyFilterFactory>(filter),
filter => Assert.IsType<AutoValidateAntiforgeryTokenAttribute>(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);
}
}
}

View File

@ -19,7 +19,8 @@ namespace RazorPagesWebSite
options.AuthorizePage("/HelloWorldWithAuth");
options.AuthorizeFolder("/Pages/Admin");
options.AllowAnonymousToPage("/Pages/Admin/Login");
});
})
.WithRazorPagesAtContentRoot();
}
public void Configure(IApplicationBuilder app)

View File

@ -16,7 +16,6 @@ namespace RazorPagesWebSite
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{
options.RootDirectory = "/Pages";
options.AuthorizePage("/Conventions/Auth");
options.AuthorizeFolder("/Conventions/AuthFolder");
});