Usm*_*bal 4 fluentvalidation asp.net-core-webapi
我使用的库"FluentValidation.AspNetCore": "6.4.0-beta3"中.netcore WebApi的一个项目。您可以在下面看到项目结构。如果我将 CurrencyDTO.cs代码放在第2节(Project FH.WebAPI)中,并且如果将相同的代码放在第1节(Class Library DTO)中,则库工作正常。要求是我必须将代码放置在Class库中FH.Common。周围有什么解决办法。我已经搜索了,但是没有找到任何东西
项目结构
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.");
}
}
Run Code Online (Sandbox Code Playgroud)
库配置
步骤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%" ]
}
}
Run Code Online (Sandbox Code Playgroud)
第2步)。在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(options => { }).AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
// Other code..
}
Run Code Online (Sandbox Code Playgroud)
用于控制器
[HttpPost]
[Route("CreateCurrency")]
public IActionResult CreateCurrency([FromBody] CurrencyDTO model)
{
if (!ModelState.IsValid) //<----Validate here
{
return new BadRequestObjectResult(ModelState);
}
//Other Code..
}
Run Code Online (Sandbox Code Playgroud)
问题出在注册startup.cs热线,感谢@JeremySkinner给我建议了正确的方法,我在此引用他的回答。
我的错
services.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
Run Code Online (Sandbox Code Playgroud)
用。。。来代替
RegisterValidatorsFromAssemblyContaining<Startup>() to RegisterValidatorsFromAssemblyContaining<CurrencyDTOValidator>()
Run Code Online (Sandbox Code Playgroud)
链接说明
上面的链接是@JeremySkinner发布的答案
| 归档时间: |
|
| 查看次数: |
2289 次 |
| 最近记录: |