我有一个域驱动设计解决方案,由于某种原因,当通过GateWay进行 API 调用时,我在运行时遇到此异常:
One or more errors occurred. (Method not found: 'Void FluentValidation.AbstractValidator`1.When(System.Func`2<!0,Boolean>, System.Action)'.)
Run Code Online (Sandbox Code Playgroud)
出现错误如下:

我有这样的解决方案:

我现在关注的主要 4 个项目是:
Web.ClientSite发出请求,Service.Api.Gateway然后调用Account.Api. 请注意,到处都Core.Model引用了
非常重要:如果我从 中删除 FluentValidation 的引用Core.Model,异常就会消失。
我希望这些信息足够了。您认为我为什么会遇到此异常以及如何消除。
我在 ASP.NET Core 6 Web API 项目中使用FluentValidation 。这对于大多数情况都适用,因此:
但有一个具体情况是有问题的:
string而不是string?)并且请求对象包含其 null 值,则验证由 ASP.NET Core 完成(但应由 FluentValidation 完成)。我当前的解决方法是注释所有不可为空的属性,[ValidateNever]以便 ASP.NET Core 忽略它们,但这并不好。
有没有办法禁用不可为空属性的 ASP.NET Core 模型属性验证?
注意:我无法完全禁用 ASP.NET Core 验证,因为那样它甚至不会返回 JSON 语法错误的验证错误结果。
我有以下代码:
public class NewsEditViewDataValidator : AbstractValidator<NewsEditViewData>
{
public NewsEditViewDataValidator()
{
// Status unique identifier cannot be empty
// Status unique identifier must be greater or equal to 1
RuleFor(x => x.StatusId)
.NotEmpty()
.WithMessage("Status is required")
.GreaterThanOrEqualTo(1)
.WithMessage("Status unique identifier must be greater or equal to 1");
// Other rule sets
}
}
Run Code Online (Sandbox Code Playgroud)
StatusId是一个整数.NotEmpty在这种情况下如何工作?它验证了什么?整数还是字符串?对于此部分检查整数是否为空,单元测试会是什么样的?
这用于验证我的MVC 3应用程序中的下拉列表.验证在视图上运行良好.GreaterThanOrEqualTo部分是状态唯一标识符永远不会小于1.这我想触发验证我的对象.什么时候这样做NotEmpty也不会开火?是否首先要解雇哪一个?如果StatusId为0,哪个规则集将触发?如果是-1?我希望NotEmpty在检查业务对象的id时使用视图和GreaterThanOrEqualTo.有什么建议?
我在ASP.NET MVC 3应用程序中使用FluentValidation.
我的视图模型中有一个MaxNumberTeamMembers属性:
/// <summary>
/// Gets or sets the maximum number of team members.
/// </summary>
public int MaxNumberTeamMembers { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想知道以下规则集是否可行:
上面的规则集会是什么样的?
我有以下内容,但如果我输入0,它在GreaterThan部分不起作用:
RuleFor(x => x.MaxNumberTeamMembers)
.NotEmpty()
.WithMessage("Max. number of team members is required")
.GreaterThan(0)
.WithMessage("Max. number of team members must be greater than 0");
Run Code Online (Sandbox Code Playgroud)
更新2011-02-14:
RuleFor(x => x.MinNumberCharactersCitation)
.NotNull()
.WithMessage("Min. number of characters for citation is required")
.GreaterThanOrEqualTo(1)
.WithMessage("Min. number of characters for citation must be greater than or equal to 1") …Run Code Online (Sandbox Code Playgroud) 我一直在用asp.net mvc,nhibernate和ddd概念开发一个web应用程序.
我已经为我的域类开发了Fluent验证的验证,它工作正常.那么,现在,我需要一个ViewModel来编辑View中的实体,所以我的问题是,我是否需要创建另一个验证类来验证我的viewmodel?或者我应该怎么做以解决这种情况?
我问它是因为我不想打破DRY(不要重复自己)concetp.
谢谢!
validation asp.net-mvc domain-driven-design viewmodel fluentvalidation
我在我的MVC项目中使用FluentValidation并具有以下模型和验证器:
[Validator(typeof(CreateNoteModelValidator))]
public class CreateNoteModel {
public string NoteText { get; set; }
}
public class CreateNoteModelValidator : AbstractValidator<CreateNoteModel> {
public CreateNoteModelValidator() {
RuleFor(m => m.NoteText).NotEmpty();
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个控制器动作来创建注释:
public ActionResult Create(CreateNoteModel model) {
if( !ModelState.IsValid ) {
return PartialView("Test", model);
// save note here
return Json(new { success = true }));
}
Run Code Online (Sandbox Code Playgroud)
我写了一个单元测试来验证行为:
[Test]
public void Test_Create_With_Validation_Error() {
// Arrange
NotesController controller = new NotesController();
CreateNoteModel model = new CreateNoteModel();
// Act
ActionResult result = controller.Create(model);
// Assert
Assert.IsInstanceOfType(result, …Run Code Online (Sandbox Code Playgroud) 我正在使用NinjectHttpApplication我的项目中定义的几个模块.
我想要的是创建FluentValidation验证工厂,如http://www.thekip.nl/2011/09/22/using-fluentvalidation-for-both-domain-validation-and-validation-in-mvc-projects/中所述.
要创建一个具体的验证工厂,我需要覆盖
IValidator CreateInstance(Type validatorType)
Run Code Online (Sandbox Code Playgroud)
然后我应该打电话的方法
return kernel.Get<validatorType>() as IValidator
Run Code Online (Sandbox Code Playgroud)
但我已经读过,Global.asax建议不要在范围之外使用IKernel .
有什么选择可以制作我想要的东西?
编辑:使用Ninject-FluentValidation扩展
正如雷莫所说,GitHub(https://github.com/ninject/ninject.web.mvc.fluentvalidation)有一个扩展名.扩展中有一个类:
public class NinjectValidatorFactory : ValidatorFactoryBase { ... }
Run Code Online (Sandbox Code Playgroud)
它接受IKernel构造函数并创建实例IValidator
public override IValidator CreateInstance(Type validatorType)
{
if(((IList<IBinding>)Kernel.GetBindings(validatorType)).Count == 0)
{
return null;
}
return Kernel.Get(validatorType) as IValidator;
}
Run Code Online (Sandbox Code Playgroud)
然后我的代码就像:
public class MvcApplication : NinjectHttpApplication
{
private NinjectValidatorFactory nvfactory;
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void …Run Code Online (Sandbox Code Playgroud) public class ProspectValidator : AbstractValidator<Prospect>
{
public ProspectValidator()
{
RuleFor(p => p.CompetitorProducts)
.NotNull()
.When(p => !p.ExistingCustomer);
RuleFor(p => p.CompetitorProducts.Count)
.GreaterThan(0)
.When(p => p.CompetitorProducts != null && !p.ExistingCustomer);
}
}
Run Code Online (Sandbox Code Playgroud)
该验证器检查是否ExistingCustomer为false,则不CompetitorProducts为null并具有至少一个元素。
它可以工作,但是有可能将其写为一条规则吗?
我通过使用针对FluentValidation的webapi集成包,设置了一个Web api项目以使用FluentValidation。然后,我创建了一个验证器,该验证器CustomAsync(...)用于对数据库运行查询。
问题在于,等待数据库任务时验证似乎陷入僵局。我进行了一些调查,看来MVC ModelState API是同步的,并且它调用Validate(...)使FluentValidation 调用的同步方法task.Result,从而导致死锁。
假设异步调用无法与Webapi集成验证一起使用是否正确?
如果是这样,还有什么选择?WebApi ActionFilters似乎支持异步处理。我是否需要构建自己的过滤器来手动处理验证,或者是否已经存在我没有看到的东西?
c# model-validation fluentvalidation async-await asp.net-web-api
参考:下面的(基本上)代码的.NET提琴。
我正在尝试使用FluentValidation 测试a string是否有效Uri:
public class LinksValidator : AbstractValidator<string>
{
public LinksValidator()
{
RuleFor(x => x)
.Must(LinkMustBeAUri)
.WithMessage("Link '{PropertyValue}' must be a valid URI. eg: http://www.SomeWebSite.com.au");
}
private static bool LinkMustBeAUri(string link)
{
if (string.IsNullOrWhiteSpace(link))
{
return false;
}
Uri result;
return Uri.TryCreate(link, UriKind.Absolute, out result);
}
}
Run Code Online (Sandbox Code Playgroud)
并进行验证测试...
public class LinksValidatorTests
{
private readonly LinksValidator _linksValidator;
public LinksValidatorTests()
{
_linksValidator = new LinksValidator();
}
[Theory]
[InlineData("Http://www.SomeDomain.com")]
[InlineData("https://www.SomeDomain.com")]
[InlineData("http://www.SomeDomain.com.au")]
public void GivenAValidUri_Validate_ShouldNotHaveAValidationError(string uri)
{
// …Run Code Online (Sandbox Code Playgroud) fluentvalidation ×10
asp.net-mvc ×4
c# ×4
validation ×2
.net ×1
api-gateway ×1
asp.net-core ×1
async-await ×1
exception ×1
ninject ×1
unit-testing ×1
viewmodel ×1