Validator.TryValidateObject不验证属性 [英] Validator.TryValidateObject does not validate attribute

查看:1263
本文介绍了Validator.TryValidateObject不验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的WebAPI V2我已经建立了一些把一个对象作为参数。我同时使用IValidateableObject和dataannotations对我触发使用的WebAPI过滤模型验证。

Using WebApi v2 I've built something that takes an object as argument. I'm using both IValidateableObject and dataannotations for model validations that I trigger using an WebApi filter.

然而,一个对象包含所有需要验证项目的阵列。我为了做到这一点使用像 Asp.net中创建自定义属性网页API嵌套模型验证,但我不能得到验证去。此外 - 我已经使用ValidateAllProperties标志

However, one object contains an array of items that all need to be validated. I've created a custom attribute in order to do this using something like Asp.net Web Api nested model validation, but I can't get the validation going. Also - I've use the ValidateAllProperties flag already.

所以,我建了一个控制台应用程序来验证行为和验证似乎并没有被触发(或我错误地调用API)。这里的东西不为我工作:

So I built a console app to verify the behavior and the validation doesn't seem to be triggered (or I'm calling the API incorrectly). Here's something that does not work for me:

命名空间的ConsoleApplication1
{
    使用系统;
    使用System.Collections.Generic;
    使用System.ComponentModel.DataAnnotations;

namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations;

class Program
{
    static void Main()
    {
        var s = new Boundary { LowerDecimal = 1.1m };
        var isValid = Validator.TryValidateObject(s, new ValidationContext(s, null, null), new List<ValidationResult>(), true);
        Console.WriteLine("Validation resulted in " + isValid);
        Console.ReadLine();
    }
}

public class Boundary
{
    [Range(0,1)]
    public decimal LowerDecimal { get; set; }
}

}

isValid方法总是返回true。我在做什么错在这里?

isValid always returns true. What am I doing wrong here?

编辑 - 还试图确定喜欢这里的元数据类:<一href=\"http://stackoverflow.com/questions/2422031/validation-does-not-work-when-i-use-validator-tryvalidateobject\">Validation当我用不工作Validator.TryValidateObject

EDIT - Also tried defining MetaData class like here: Validation does not work when I use Validator.TryValidateObject

推荐答案

TryValidateObject 是一个红色的鲱鱼,实际上,问题在于用 RangeAttribute

The TryValidateObject is a red herring, the problem actually lies with the RangeAttribute.

问题是属性有2个重载(一个用于 INT 和一个双击),在你的属性定义你声明 INT 值,这意味着内部的属性值转换为 INT 这将截断浮点 - 所以在本质上 1.1 将被截断为 1 所以总是会在范围内。

The problem is the attribute has 2 overloads (one for int and one for double), in your attribute definition you have declared int values which means internally the property value is cast to int which will truncate the floating point - so in essence 1.1 will be truncated to 1 so will always be in range.

简单的修复,确保你的属性,知道你要比较双和不是 INT

Simple fix, make sure your attribute knows you want to compare double's and not int i.e.

[Range(0.0, 1.0)]
public decimal LowerDecimal { get; set; }

这篇关于Validator.TryValidateObject不验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆