[MVC][Fixes #7576]HtmlHelperOptions.ClientValidationEnabled = false is ignored in Razor Pages (#9289)

* Replaced IOptions<HtmlHelperOptions> with IOptions<MvcViewOptions> on the PageActionInvokerProvider.
This commit is contained in:
Javier Calvarro Nelson 2019-04-12 11:56:24 +02:00 committed by GitHub
parent f934bfaa7e
commit 3265c54518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 7 deletions

View File

@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
private readonly IModelMetadataProvider _modelMetadataProvider;
private readonly ITempDataDictionaryFactory _tempDataFactory;
private readonly MvcOptions _mvcOptions;
private readonly HtmlHelperOptions _htmlHelperOptions;
private readonly MvcViewOptions _mvcViewOptions;
private readonly IPageHandlerMethodSelector _selector;
private readonly DiagnosticListener _diagnosticListener;
private readonly ILogger<PageActionInvoker> _logger;
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
IModelBinderFactory modelBinderFactory,
ITempDataDictionaryFactory tempDataFactory,
IOptions<MvcOptions> mvcOptions,
IOptions<HtmlHelperOptions> htmlHelperOptions,
IOptions<MvcViewOptions> mvcViewOptions,
IPageHandlerMethodSelector selector,
DiagnosticListener diagnosticListener,
ILoggerFactory loggerFactory,
@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
modelBinderFactory,
tempDataFactory,
mvcOptions,
htmlHelperOptions,
mvcViewOptions,
selector,
diagnosticListener,
loggerFactory,
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
IModelBinderFactory modelBinderFactory,
ITempDataDictionaryFactory tempDataFactory,
IOptions<MvcOptions> mvcOptions,
IOptions<HtmlHelperOptions> htmlHelperOptions,
IOptions<MvcViewOptions> mvcViewOptions,
IPageHandlerMethodSelector selector,
DiagnosticListener diagnosticListener,
ILoggerFactory loggerFactory,
@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
_modelMetadataProvider = modelMetadataProvider;
_tempDataFactory = tempDataFactory;
_mvcOptions = mvcOptions.Value;
_htmlHelperOptions = htmlHelperOptions.Value;
_mvcViewOptions = mvcViewOptions.Value;
_selector = selector;
_diagnosticListener = diagnosticListener;
_logger = loggerFactory.CreateLogger<PageActionInvoker>();
@ -214,7 +214,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
cacheEntry,
_parameterBinder,
_tempDataFactory,
_htmlHelperOptions);
_mvcViewOptions.HtmlHelperOptions);
}
private PageActionInvokerCacheEntry CreateCacheEntry(

View File

@ -532,7 +532,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
modelBinderFactory,
tempDataFactory.Object,
Options.Create(mvcOptions),
Options.Create(new HtmlHelperOptions()),
Options.Create(new MvcViewOptions()),
Mock.Of<IPageHandlerMethodSelector>(),
new DiagnosticListener("Microsoft.AspNetCore"),
NullLoggerFactory.Instance,

View File

@ -0,0 +1,34 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{
public class ClientValidationOptionsTests : IClassFixture<MvcTestFixture<RazorPagesWebSite.Startup>>
{
public ClientValidationOptionsTests(MvcTestFixture<RazorPagesWebSite.Startup> fixture) =>
Fixture = fixture;
public MvcTestFixture<RazorPagesWebSite.Startup> Fixture { get; }
[Fact]
public async Task DisablingClientValidation_DisablesItForPagesAndViews()
{
// Arrange
var client = Fixture
.WithWebHostBuilder(whb => whb.UseStartup<RazorPagesWebSite.StartupWithClientValidationDisabled>())
.CreateClient();
// Act
var view = await client.GetStringAsync("Controller/ClientValidationDisabled");
var page = await client.GetStringAsync("ClientvalidationDisabled");
// Assert
Assert.Equal("ClientValidationDisabled", view);
Assert.Equal("ClientValidationDisabled", page);
}
}
}

View File

@ -0,0 +1,13 @@
// 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 Microsoft.AspNetCore.Mvc;
namespace RazorPagesWebSite
{
public class ClientValidationDisabledController : Controller
{
[HttpGet("/Controller/ClientValidationDisabled")]
public IActionResult ValidationDisabled() => View();
}
}

View File

@ -0,0 +1,2 @@
@page
@(ViewContext.ClientValidationEnabled ? "ClientValidationEnabled" : "ClientValidationDisabled")

View File

@ -0,0 +1,41 @@
// 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 Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace RazorPagesWebSite
{
public class StartupWithClientValidationDisabled
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => options.LoginPath = "/Login");
services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/Admin");
})
.SetCompatibilityVersion(CompatibilityVersion.Latest);
services.Configure<MvcViewOptions>(o => o.HtmlHelperOptions.ClientValidationEnabled = false);
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}
}

View File

@ -0,0 +1 @@
@(ViewContext.ClientValidationEnabled ? "ClientValidationEnabled" : "ClientValidationDisabled")