[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:
parent
f934bfaa7e
commit
3265c54518
|
|
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
private readonly IModelMetadataProvider _modelMetadataProvider;
|
private readonly IModelMetadataProvider _modelMetadataProvider;
|
||||||
private readonly ITempDataDictionaryFactory _tempDataFactory;
|
private readonly ITempDataDictionaryFactory _tempDataFactory;
|
||||||
private readonly MvcOptions _mvcOptions;
|
private readonly MvcOptions _mvcOptions;
|
||||||
private readonly HtmlHelperOptions _htmlHelperOptions;
|
private readonly MvcViewOptions _mvcViewOptions;
|
||||||
private readonly IPageHandlerMethodSelector _selector;
|
private readonly IPageHandlerMethodSelector _selector;
|
||||||
private readonly DiagnosticListener _diagnosticListener;
|
private readonly DiagnosticListener _diagnosticListener;
|
||||||
private readonly ILogger<PageActionInvoker> _logger;
|
private readonly ILogger<PageActionInvoker> _logger;
|
||||||
|
|
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
IModelBinderFactory modelBinderFactory,
|
IModelBinderFactory modelBinderFactory,
|
||||||
ITempDataDictionaryFactory tempDataFactory,
|
ITempDataDictionaryFactory tempDataFactory,
|
||||||
IOptions<MvcOptions> mvcOptions,
|
IOptions<MvcOptions> mvcOptions,
|
||||||
IOptions<HtmlHelperOptions> htmlHelperOptions,
|
IOptions<MvcViewOptions> mvcViewOptions,
|
||||||
IPageHandlerMethodSelector selector,
|
IPageHandlerMethodSelector selector,
|
||||||
DiagnosticListener diagnosticListener,
|
DiagnosticListener diagnosticListener,
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
|
|
@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
modelBinderFactory,
|
modelBinderFactory,
|
||||||
tempDataFactory,
|
tempDataFactory,
|
||||||
mvcOptions,
|
mvcOptions,
|
||||||
htmlHelperOptions,
|
mvcViewOptions,
|
||||||
selector,
|
selector,
|
||||||
diagnosticListener,
|
diagnosticListener,
|
||||||
loggerFactory,
|
loggerFactory,
|
||||||
|
|
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
IModelBinderFactory modelBinderFactory,
|
IModelBinderFactory modelBinderFactory,
|
||||||
ITempDataDictionaryFactory tempDataFactory,
|
ITempDataDictionaryFactory tempDataFactory,
|
||||||
IOptions<MvcOptions> mvcOptions,
|
IOptions<MvcOptions> mvcOptions,
|
||||||
IOptions<HtmlHelperOptions> htmlHelperOptions,
|
IOptions<MvcViewOptions> mvcViewOptions,
|
||||||
IPageHandlerMethodSelector selector,
|
IPageHandlerMethodSelector selector,
|
||||||
DiagnosticListener diagnosticListener,
|
DiagnosticListener diagnosticListener,
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
|
|
@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
_modelMetadataProvider = modelMetadataProvider;
|
_modelMetadataProvider = modelMetadataProvider;
|
||||||
_tempDataFactory = tempDataFactory;
|
_tempDataFactory = tempDataFactory;
|
||||||
_mvcOptions = mvcOptions.Value;
|
_mvcOptions = mvcOptions.Value;
|
||||||
_htmlHelperOptions = htmlHelperOptions.Value;
|
_mvcViewOptions = mvcViewOptions.Value;
|
||||||
_selector = selector;
|
_selector = selector;
|
||||||
_diagnosticListener = diagnosticListener;
|
_diagnosticListener = diagnosticListener;
|
||||||
_logger = loggerFactory.CreateLogger<PageActionInvoker>();
|
_logger = loggerFactory.CreateLogger<PageActionInvoker>();
|
||||||
|
|
@ -214,7 +214,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
cacheEntry,
|
cacheEntry,
|
||||||
_parameterBinder,
|
_parameterBinder,
|
||||||
_tempDataFactory,
|
_tempDataFactory,
|
||||||
_htmlHelperOptions);
|
_mvcViewOptions.HtmlHelperOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PageActionInvokerCacheEntry CreateCacheEntry(
|
private PageActionInvokerCacheEntry CreateCacheEntry(
|
||||||
|
|
|
||||||
|
|
@ -532,7 +532,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
modelBinderFactory,
|
modelBinderFactory,
|
||||||
tempDataFactory.Object,
|
tempDataFactory.Object,
|
||||||
Options.Create(mvcOptions),
|
Options.Create(mvcOptions),
|
||||||
Options.Create(new HtmlHelperOptions()),
|
Options.Create(new MvcViewOptions()),
|
||||||
Mock.Of<IPageHandlerMethodSelector>(),
|
Mock.Of<IPageHandlerMethodSelector>(),
|
||||||
new DiagnosticListener("Microsoft.AspNetCore"),
|
new DiagnosticListener("Microsoft.AspNetCore"),
|
||||||
NullLoggerFactory.Instance,
|
NullLoggerFactory.Instance,
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
@page
|
||||||
|
@(ViewContext.ClientValidationEnabled ? "ClientValidationEnabled" : "ClientValidationDisabled")
|
||||||
|
|
@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
@(ViewContext.ClientValidationEnabled ? "ClientValidationEnabled" : "ClientValidationDisabled")
|
||||||
Loading…
Reference in New Issue