gpt4 book ai didi

FluentValidation.AspNetCore 在类库中不起作用

转载 作者:行者123 更新时间:2023-12-05 00:16:22 31 4
gpt4 key购买 nike

我正在使用图书馆 "FluentValidation.AspNetCore": "6.4.0-beta3".netcore WebApi在一个项目中。您可以在下面看到项目结构。如果我放置 CurrencyDTO.cs,图书馆工作正常第 2 节(项目 FH.WebAPI)中的代码,如果放在第 1 节(类库 DTO)中的相同代码则不起作用。并且要求是我必须将代码放在类库中 FH.Common .有没有解决办法。我有搜索,但没有找到任何东西

项目结构

Project Structure

CurrencyDTO.cs

[Validator(typeof(CurrencyDTOValidator))]
public class CurrencyDTO
{
public int Id { get { return CurrencyId; } }
public int CurrencyId { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }

}

public class CurrencyDTOValidator : AbstractValidator<CurrencyDTO>
{
public CurrencyDTOValidator()
{
RuleFor(x => x.Name).NotEmpty().NotNull().WithMessage("The currency 'Name' is required.")
.Length(0, 250).WithMessage("The currency 'Name' cannot be more than 250 characters.");

RuleFor(x => x.Symbol).NotEmpty().WithMessage("The currency Symbol is required.");


}
}

库配置

第1步) 。加入 project.json
{
"dependencies": {
"Autofac": "4.3.0",
"Autofac.Extensions.DependencyInjection": "4.0.0",
"AutoMapper": "5.2.0",
"EntityFramework": "6.1.3",
"FH.Business": "1.0.0-*",
"FH.Common": "1.0.0-*",
"JWT": "1.4.1-beta",
"Microsoft.AspNet.Http.Extensions": "1.0.0-rc1-final",
"Microsoft.AspNet.WebApi.Client": "5.2.3",
"Microsoft.AspNet.WebApi.Core": "5.2.3",
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.1.0",
"Microsoft.AspNetCore.Diagnostics": "1.1.0",
"Microsoft.AspNetCore.Mvc": "1.1.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
//other libraries..
"FluentValidation.AspNetCore": "6.4.0-beta3" //<------Here
},

"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},

"frameworks": {
"net461": {
"frameworkAssemblies": {
"System.Drawing": "4.0.0.0"
}
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/*.cshtml",
"appsettings.json",
"web.config"
]
},

"scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

第2步)。在 Startup.cs
   public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(options => { }).AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
// Other code..
}

用于 Controller
[HttpPost]
[Route("CreateCurrency")]
public IActionResult CreateCurrency([FromBody] CurrencyDTO model)
{
if (!ModelState.IsValid) //<----Validate here
{
return new BadRequestObjectResult(ModelState);
}
//Other Code..
}

最佳答案

问题出在 startup.cs 中的注册行中并感谢@JeremySkinner,他以正确的方式向我提出了建议,我在这里引用了他的回答。

我的错误

services.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());

替换
RegisterValidatorsFromAssemblyContaining<Startup>() 


RegisterValidatorsFromAssemblyContaining<CurrencyDTOValidator>()

Actual link to the Issue

链接说明

上面的链接是@JeremySkinner 发布的答案

关于FluentValidation.AspNetCore 在类库中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42026030/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com