parent
04fd762943
commit
ccdaa5a729
|
|
@ -16,7 +16,7 @@ namespace MvcSandbox
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddMvc().AddCookieTempDataProvider();
|
services.AddMvc();
|
||||||
|
|
||||||
services.Insert(0, ServiceDescriptor.Singleton(
|
services.Insert(0, ServiceDescriptor.Singleton(
|
||||||
typeof(IConfigureOptions<AntiforgeryOptions>),
|
typeof(IConfigureOptions<AntiforgeryOptions>),
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Mvc.Razor;
|
using Microsoft.AspNetCore.Mvc.Razor;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
|
@ -35,5 +36,48 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
builder.Services.Configure(setupAction);
|
builder.Services.Configure(setupAction);
|
||||||
return builder;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,28 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
return builder;
|
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)
|
private static void AddFeatureProviders(IMvcCoreBuilder builder)
|
||||||
{
|
{
|
||||||
if (!builder.PartManager.FeatureProviders.OfType<CompiledPageFeatureProvider>().Any())
|
if (!builder.PartManager.FeatureProviders.OfType<CompiledPageFeatureProvider>().Any())
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
|
||||||
|
|
||||||
// Always require an antiforgery token on post
|
// Always require an antiforgery token on post
|
||||||
options.ConfigureFilter(new AutoValidateAntiforgeryTokenAttribute());
|
options.ConfigureFilter(new AutoValidateAntiforgeryTokenAttribute());
|
||||||
|
|
||||||
|
options.RootDirectory = "/Pages";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task RazorPages_ReturnSuccess()
|
public async Task RazorPages_ReturnSuccess()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetStringAsync("http://localhost/Pages/");
|
var response = await Client.GetStringAsync("http://localhost/PagesHome");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Contains("This file should give you a quick view of a Mvc Razor Page in action.", response);
|
Assert.Contains("This file should give you a quick view of a Mvc Razor Page in action.", response);
|
||||||
|
|
|
||||||
|
|
@ -29,5 +29,19 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
|
||||||
filter => Assert.IsType<PageSaveTempDataPropertyFilterFactory>(filter),
|
filter => Assert.IsType<PageSaveTempDataPropertyFilterFactory>(filter),
|
||||||
filter => Assert.IsType<AutoValidateAntiforgeryTokenAttribute>(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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ namespace RazorPagesWebSite
|
||||||
options.AuthorizePage("/HelloWorldWithAuth");
|
options.AuthorizePage("/HelloWorldWithAuth");
|
||||||
options.AuthorizeFolder("/Pages/Admin");
|
options.AuthorizeFolder("/Pages/Admin");
|
||||||
options.AllowAnonymousToPage("/Pages/Admin/Login");
|
options.AllowAnonymousToPage("/Pages/Admin/Login");
|
||||||
});
|
})
|
||||||
|
.WithRazorPagesAtContentRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ namespace RazorPagesWebSite
|
||||||
.AddCookieTempDataProvider()
|
.AddCookieTempDataProvider()
|
||||||
.AddRazorPagesOptions(options =>
|
.AddRazorPagesOptions(options =>
|
||||||
{
|
{
|
||||||
options.RootDirectory = "/Pages";
|
|
||||||
options.AuthorizePage("/Conventions/Auth");
|
options.AuthorizePage("/Conventions/Auth");
|
||||||
options.AuthorizeFolder("/Conventions/AuthFolder");
|
options.AuthorizeFolder("/Conventions/AuthFolder");
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue