Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Make /Pages the default root for pages.
Browse files Browse the repository at this point in the history
Fixes #6184
  • Loading branch information
pranavkm committed Apr 24, 2017
1 parent 04fd762 commit ccdaa5a
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion samples/MvcSandbox/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Startup
// 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>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,5 +36,48 @@ public static IMvcBuilder AddRazorPagesOptions(
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ public static IMvcCoreBuilder AddRazorPages(
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public void Configure(RazorPagesOptions options)

// Always require an antiforgery token on post
options.ConfigureFilter(new AutoValidateAntiforgeryTokenAttribute());

options.RootDirectory = "/Pages";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Home_Pages_ReturnSuccess()
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,19 @@ public void Configure_AddsGlobalFilters()
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);
}
}
}
3 changes: 2 additions & 1 deletion test/WebSites/RazorPagesWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public void ConfigureServices(IServiceCollection services)
options.AuthorizePage("/HelloWorldWithAuth");
options.AuthorizeFolder("/Pages/Admin");
options.AllowAnonymousToPage("/Pages/Admin/Login");
});
})
.WithRazorPagesAtContentRoot();
}

public void Configure(IApplicationBuilder app)
Expand Down
1 change: 0 additions & 1 deletion test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public void ConfigureServices(IServiceCollection services)
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{
options.RootDirectory = "/Pages";
options.AuthorizePage("/Conventions/Auth");
options.AuthorizeFolder("/Conventions/AuthFolder");
});
Expand Down

0 comments on commit ccdaa5a

Please sign in to comment.