我必须忽略在我创建的基本服务堆栈应用程序中启动流利验证的一些事情。我一直在关注此处找到的示例。对于我的一生,我似乎无法让我的验证者被解雇????面包屑,一定有什么愚蠢的东西我错过了......???
我正在针对用户服务(http://my.service/users)发出用户请求,该请求直接通过而无需调用已注册的适当验证器。
请求是:{"Name":"","Company":"Co","Age":10,"Count":110,"Address":"123 brown str."}
响应:“用户已保存...”
这是代码: 1.DTO
[Route("/users")]
public class User
{
public string Name { get; set; }
public string Company { get; set; }
public int Age { get; set; }
public int Count { get; set; }
public string Address { get; set; }
}
2.验证者
public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(r => r.Name).NotEmpty();
RuleFor(r => r.Age).GreaterThan(0);
}
}
3.AppHostBase
public class ValidationAppHost : AppHostBase
{
public ValidationAppHost()
: base("Validation Test", typeof(UserService).Assembly)
{
}
public override void Configure(Funq.Container container)
{
Plugins.Add(new ValidationFeature());
//This method scans the assembly for validators
container.RegisterValidators(typeof(UserValidator).Assembly);
}
}
4.服务
public class UserService : Service
{
public object Any(User user)
{
return "user saved...";
}
}
5.Global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
new ValidationAppHost().Init();
}