From 3265c545187ed025dbed301a11b6ee193b65663d Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Fri, 12 Apr 2019 11:56:24 +0200 Subject: [PATCH] [MVC][Fixes #7576]HtmlHelperOptions.ClientValidationEnabled = false is ignored in Razor Pages (#9289) * Replaced IOptions with IOptions on the PageActionInvokerProvider. --- .../PageActionInvokerProvider.cs | 12 +++--- .../PageActionInvokerProviderTest.cs | 2 +- .../ClientValidationOptionsTests.cs | 34 +++++++++++++++ .../ClientValidationDisabledController.cs | 13 ++++++ .../Pages/ClientValidationDisabled.cshtml | 2 + .../StartupWithClientValidationDisabled.cs | 41 +++++++++++++++++++ .../ValidationDisabled.cshtml | 1 + 7 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 src/Mvc/test/Mvc.FunctionalTests/ClientValidationOptionsTests.cs create mode 100644 src/Mvc/test/WebSites/RazorPagesWebSite/Controllers/ClientValidationDisabledController.cs create mode 100644 src/Mvc/test/WebSites/RazorPagesWebSite/Pages/ClientValidationDisabled.cshtml create mode 100644 src/Mvc/test/WebSites/RazorPagesWebSite/StartupWithClientValidationDisabled.cs create mode 100644 src/Mvc/test/WebSites/RazorPagesWebSite/Views/ClientValidationDisabled/ValidationDisabled.cshtml diff --git a/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageActionInvokerProvider.cs b/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageActionInvokerProvider.cs index 7e8cd9be60..08c388392c 100644 --- a/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageActionInvokerProvider.cs +++ b/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageActionInvokerProvider.cs @@ -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 _logger; @@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure IModelBinderFactory modelBinderFactory, ITempDataDictionaryFactory tempDataFactory, IOptions mvcOptions, - IOptions htmlHelperOptions, + IOptions 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, - IOptions htmlHelperOptions, + IOptions 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(); @@ -214,7 +214,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure cacheEntry, _parameterBinder, _tempDataFactory, - _htmlHelperOptions); + _mvcViewOptions.HtmlHelperOptions); } private PageActionInvokerCacheEntry CreateCacheEntry( diff --git a/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageActionInvokerProviderTest.cs b/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageActionInvokerProviderTest.cs index 550c66e8c7..fd6458d3f0 100644 --- a/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageActionInvokerProviderTest.cs +++ b/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageActionInvokerProviderTest.cs @@ -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(), new DiagnosticListener("Microsoft.AspNetCore"), NullLoggerFactory.Instance, diff --git a/src/Mvc/test/Mvc.FunctionalTests/ClientValidationOptionsTests.cs b/src/Mvc/test/Mvc.FunctionalTests/ClientValidationOptionsTests.cs new file mode 100644 index 0000000000..4b728c2979 --- /dev/null +++ b/src/Mvc/test/Mvc.FunctionalTests/ClientValidationOptionsTests.cs @@ -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> + { + public ClientValidationOptionsTests(MvcTestFixture fixture) => + Fixture = fixture; + + public MvcTestFixture Fixture { get; } + + [Fact] + public async Task DisablingClientValidation_DisablesItForPagesAndViews() + { + // Arrange + var client = Fixture + .WithWebHostBuilder(whb => whb.UseStartup()) + .CreateClient(); + + // Act + var view = await client.GetStringAsync("Controller/ClientValidationDisabled"); + var page = await client.GetStringAsync("ClientvalidationDisabled"); + + // Assert + Assert.Equal("ClientValidationDisabled", view); + Assert.Equal("ClientValidationDisabled", page); + } + } +} diff --git a/src/Mvc/test/WebSites/RazorPagesWebSite/Controllers/ClientValidationDisabledController.cs b/src/Mvc/test/WebSites/RazorPagesWebSite/Controllers/ClientValidationDisabledController.cs new file mode 100644 index 0000000000..4f6b721c56 --- /dev/null +++ b/src/Mvc/test/WebSites/RazorPagesWebSite/Controllers/ClientValidationDisabledController.cs @@ -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(); + } +} diff --git a/src/Mvc/test/WebSites/RazorPagesWebSite/Pages/ClientValidationDisabled.cshtml b/src/Mvc/test/WebSites/RazorPagesWebSite/Pages/ClientValidationDisabled.cshtml new file mode 100644 index 0000000000..5261d1c054 --- /dev/null +++ b/src/Mvc/test/WebSites/RazorPagesWebSite/Pages/ClientValidationDisabled.cshtml @@ -0,0 +1,2 @@ +@page +@(ViewContext.ClientValidationEnabled ? "ClientValidationEnabled" : "ClientValidationDisabled") \ No newline at end of file diff --git a/src/Mvc/test/WebSites/RazorPagesWebSite/StartupWithClientValidationDisabled.cs b/src/Mvc/test/WebSites/RazorPagesWebSite/StartupWithClientValidationDisabled.cs new file mode 100644 index 0000000000..68cb0f6e30 --- /dev/null +++ b/src/Mvc/test/WebSites/RazorPagesWebSite/StartupWithClientValidationDisabled.cs @@ -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(o => o.HtmlHelperOptions.ClientValidationEnabled = false); + } + + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + + app.UseAuthentication(); + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + endpoints.MapRazorPages(); + }); + } + } +} diff --git a/src/Mvc/test/WebSites/RazorPagesWebSite/Views/ClientValidationDisabled/ValidationDisabled.cshtml b/src/Mvc/test/WebSites/RazorPagesWebSite/Views/ClientValidationDisabled/ValidationDisabled.cshtml new file mode 100644 index 0000000000..a388893b34 --- /dev/null +++ b/src/Mvc/test/WebSites/RazorPagesWebSite/Views/ClientValidationDisabled/ValidationDisabled.cshtml @@ -0,0 +1 @@ +@(ViewContext.ClientValidationEnabled ? "ClientValidationEnabled" : "ClientValidationDisabled") \ No newline at end of file