简体   繁体   English

如何在 .netcore 6 razor 页面中使用 FormHelper 和 FluentValidation

[英]How to use FormHelper and FluentValidation in .netcore 6 razor pages

I am moving away from mvc to razor pages but can't seem to find my way around with the validation using fluent validator and FormHelper.我正在从 mvc 转移到 razor 页面,但似乎无法使用流利的验证器和 FormHelper 进行验证。

Normally with the mvc pattern通常使用 mvc 模式

[FormValidator]
Public async Task<IActionResult> submitFormAction()
{
    // do something 
    return View();
}

With Razor page带Razor页面

 public class AddModel : PageModel
 {
     public void OnGet()
     {

     }

     [FormValidator] // *'FormValidator' cannot be applied to razor page handler method it may be applied to either Razor page model or applied globally* 
     public async Task OnPostAsync()
     {
         
     }
 }

Have you looked into the following package for integrating FluentValidation into an ASP.NET Core application?您是否查看过以下 package 以将 FluentValidation 集成到 ASP.NET 核心应用程序中?

I believe this will include the necessary extension method(s) to allow you to register FluentValidation as the service you want to leverage for performing validation.我相信这将包括必要的扩展方法,以允许您将 FluentValidation 注册为您想要用于执行验证的服务。

https://github.com/FluentValidation/FluentValidation.AspNetCore#aspnet-core-integration-for-fluentvalidation https://github.com/FluentValidation/FluentValidation.AspNetCore#aspnet-core-integration-for-fluentvalidation

You can use this git Issues:您可以使用此 git 问题:

ASP.NET Core Razor Pages - validator for PageModel not invoked, even though validators for properties are invoked properly #887 ASP.NET 核心 Razor 页面 - 未调用 PageModel 的验证器,即使正确调用了属性的验证器 #887

https://github.com/FluentValidation/FluentValidation/issues/887 https://github.com/FluentValidation/FluentValidation/issues/887

For anyone else facing the same issue.对于其他面临同样问题的人。

The problem is with FluentValidation , in previous version .netcore you could register the FluentValidation service by simply问题出在FluentValidation上,在以前的版本 .netcore 中,您可以通过简单地注册FluentValidation服务

service.AddFluentValidation(config => config.RegisterValidatorsFromAssemblyContaining<StartUp>());

with the code above .netcore uses reflection to register all the classes in StartUp.cs assembly that inherits AbstractValidator<T>使用上面的代码,.netcore 使用反射来注册StartUp.cs程序集中继承AbstractValidator<T>的所有类

but with the .netcore 6, i had to still use the code above but replaced the Startup with Program as .netcore 6 doesn't make use of StartUp.cs and still had to implicitly register the validation services但是对于 .netcore 6,我仍然必须使用上面的代码,但将Startup替换为Program ,因为 .netcore 6 不使用StartUp.cs并且仍然必须隐式注册验证服务

builder.Services.AddTransient<IValidator<SomeClass>, SomeClassValidator>();

This works for me but i have a feeling it could be simplified more because if am working on a large app and i have upto 20 validators my Program.cs won't be maintainable这对我有用,但我觉得它可以进一步简化,因为如果我正在开发一个大型应用程序并且我有多达 20 个验证器,我的Program.cs将无法维护

PS: the [FormValidator] on the action method is obsolete as using the asp-FormHelper="true" tag helper along is okay PS:操作方法上的[FormValidator]已过时,因为使用asp-FormHelper="true"标记助手是可以的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM