Skip to main content
 首页 » 编程设计

c#之ModelBinder 验证在使用反射的 getter 上中断

2024年02月20日17zhwl

我遇到了一个似乎与反射和模型联编程序验证有关的问题,尤其是 FormatterParameterBinding.ExecuteBindingAsync(..),尽管我可以使用方法来执行操作如果我可以使用属性,我会更喜欢它。

在这里,我正在寻找对模型 Binder 验证过程的一些见解,为什么我不能做我想做的事,以及我如何解决或绕过这个问题。

设置

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(); 
        } 
    } 
} 

还有一个像这样定义后 Action 的 Controller

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

用这种json调用它:

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

如果您进入操作,您可以看到断路器已正确实例化,您可以毫无问题地调用 GetPropertyInfos()。

它是如何 splinter 的

但是,如果您取消对 PropertyInfos 属性的注释,模型 Binder 验证就会中断。我添加了一个简单的示踪剂来解决这个问题。它显示了以下相关输出:

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 时,它包含这一行:

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

在 Properties 上添加 DataContract 和相关属性(如 [IgnoreDataMember])并不能解决问题。 [Bind(Exclude="Properties")] 来自 mvc 命名空间。 Linq 似乎不是问题所在,因为 LetterCount 不会破坏模型 Binder 验证。

我的问题

  1. 为什么 PropertyInfos getter 会破坏模型绑定(bind)器验证器?
  2. 这是 Asp.NET web-api 中的错误吗?
  3. 有没有办法通过属性、自定义模型绑定(bind)器验证器、服务或类似方式来防止这种破坏?

基本上我不想为整个类或 Controller 关闭模型绑定(bind)验证,但如果我可以为 Properties 属性关闭它,那就太好了!

请您参考如下方法:

作为一般规则,您应该避免使用像 PropertyInfos 这样具有其他复杂类型属性的复杂类...在用于模型绑定(bind)的类中,但您应该只使用包含模型绑定(bind)所需属性的简单类过程,而且整个对象图不应该包含循环。模型绑定(bind)过程针对不同目的(验证属性等)分析属性类型,我可能会递归分析包含在 eac 属性类型中的子属性......如此复杂的 .net 类包含循环和许多其他.net 类型可能会破坏它。 尝试使属性 PropertyInfos 成为内部属性或将其转换为一种方法,这样它就不会被任何模型 Binder 组件处理。