在使用getter反射ModelBinder的验证符 [英] ModelBinder validation breaks on getter using reflection

查看:74
本文介绍了在使用getter反射ModelBinder的验证符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我穿过这似乎与反射和模型绑定验证和问题跑了 FormatterParameterBinding.ExecuteBindingAsync(..)特别是,虽然我可以使用方法去做我想我会preFER它,如果我可以用一个属性。

I've ran across a problem which seems to be related to reflection and model binder validation, and FormatterParameterBinding.ExecuteBindingAsync(..) in particular, and though I can use a Method to do what I want I would prefer it if I could use a Property.

在这里,我正在寻找一些洞察模型绑定验证过程中,为什么我不能做我想做什么,我怎么能解决这个问题,还是围绕它得到的。

Here I'm looking for some insight into the model binder validation process, why I am unable to do what I want and how I can fix the problem, or get around it.

的设置

public class ModelBindingValidationBreaker
{
    public ModelBindingValidationBreaker()
    {
        Properties = new List<string>();
    }

    public int A { get; set; }
    public int B { get; set; }
    public IList<string> Properties { get; set; }

    /*// Uncomment to break the model binder validation!
    public IList<PropertyInfo> PropertyInfos
    {
        get
        {
            return GetType()
                .GetProperties()
                .Where(pi => Properties.Contains(pi.Name))
                .ToList();
        }
    }//*/

    public IList<PropertyInfo> GetPropertyInfos()
    {
        return GetType()
            .GetProperties()
            .Where(pi => Properties.Contains(pi.Name))
            .ToList();
    }

    public IList<int> LetterCounts
    {
        get
        {
            return Properties.Select(p => p.Length).ToList();
        }
    }
}

和与后作用的控制器是这样定义的

And a controller with a post action defined like this

public void Post(ModelBindingValidationBreaker breaker){...}

用这种JSON的调用它:

Call it with this kind of json:

{
    "Properties": [
        "A"
    ],
    "A": 1,
    "B": 2
}

如果您踏入动作就可以看到断路器正确实例,并且可以调用 GetPropertyInfos()没有任何问题。

If you step into the action you can see that the breaker is correctly instantiated and you can call GetPropertyInfos() without any issue.

如何打破

不过,如果你取消注释PropertyInfos财产,模型绑定验证休息。我添加了一个简单的示踪剂摸不着头脑。它显示了以下相关的输出:

However, if you uncomment the PropertyInfos property, the model binder validation breaks. I added a simple tracer to figure this out. It shows the following relevant output:

System.Web.Http.Action: ApiControllerActionSelector;SelectAction;Selected action 'Post(ModelBindingValidationBreaker breaker)'
System.Web.Http.ModelBinding: HttpActionBinding;ExecuteBindingAsync;
System.Web.Http.ModelBinding: FormatterParameterBinding;ExecuteBindingAsync;Binding parameter 'breaker'
System.Net.Http.Formatting: JsonMediaTypeFormatter;ReadFromStreamAsync;Type='ModelBindingValidationBreaker', content-type='application/json'
System.Net.Http.Formatting: JsonMediaTypeFormatter;ReadFromStreamAsync;Value read='OverPostCount.Models.ModelBindingValidationBreaker'
System.Web.Http.ModelBinding: FormatterParameterBinding;ExecuteBindingAsync;
System.Web.Http.ModelBinding: HttpActionBinding;ExecuteBindingAsync;
System.Web.Http.Controllers: CustomController;ExecuteAsync;
System.Net.Http.Formatting: DefaultContentNegotiator;Negotiate;Type='HttpError', formatters=[JsonMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer]

包含此行,而不是当你排除违规get_PropertyInfos:

Which contains this line instead, when you exclude the offending get_PropertyInfos:

System.Web.Http.ModelBinding: FormatterParameterBinding;ExecuteBindingAsync;Parameter 'breaker' bound to the value 'OverPostCount.Models.ModelBindingValidationBreaker'

添加 DataContract ,并在性能,如 [IgnoreDataMember] 相关属性不解决问题。无论是做 [绑定(不包括=属性)] 从MVC命名空间。 LINQ似乎并不成为问题,因为 LetterCount 不破模型绑定验证。

Adding DataContract, and related attributes like [IgnoreDataMember] on Properties does not fix the problem. Neither does [Bind(Exclude="Properties")] from the mvc namespace. Linq does not seem to be the problem, since LetterCount does not break the model binder validation.

我的提问


  1. 为什么PropertyInfos吸气打破模型绑定验证?

  2. 这是Asp.NET Web的API中的错误?

  3. 有没有一种方法,以prevent这种断裂通过的属性,定制的模型绑定验证器,服务或类似的?

基本上我并不想关闭模型绑定验证全班或控制器,但如果我可以把它关闭Properties属性,这将是伟大的!

Basically I don't want to turn off model binding validation for the whole class or controller, but if I could turn it off for the Properties property, that would be great!

推荐答案

正如你应该避免使用复杂的类一样,与其他复杂类型的属性PropertyInfos ......在用于模型绑定类一般规则,但你应该使用只是简单的类只包含您需要的模型绑定过程,而且整个对象图应该包含没有循环性能的。模型绑定过程分析用于不同目的的属性(属性验证等)的我可能recusrively分析包含在EAC属性的类型的子属性...包含毛圈如此复杂的.NET类和许多其他类型.NET类型可能会打破它。
尝试使财产PropertyInfos内部或把它变成一个方法,因此工资需不进行处理,由任何型号的粘合剂成分。

As a general rule you should avoid using complex classes like PropertyInfos that have properties with other complex types...in classes used for model binding, but you should use just simple classes that contains JUST THE PROPERTIES you need for the model binding process, moreover the whole object graph should contain no loops. Model binding process analyze the type of properties for different purposes (validation attributes, etc.) an i may recusrively analyze the sub-properties of contained in the type of eac property...so complex .net classes containing loops and a lot of other .net types may break it. Try making the property PropertyInfos internal or turn it into a method so it shgould not be processed, by any model binder component.

这篇关于在使用getter反射ModelBinder的验证符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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