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.
The setup
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){...}
Call it with this kind of json:
{
"Properties": [
"A"
],
"A": 1,
"B": 2
}
If you step into the action you can see that the breaker is correctly instantiated and you can call GetPropertyInfos()
without any issue.
How it breaks
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]
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'
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.
My questions
- Why does the PropertyInfos getter break the model binder validator?
- Is this a bug in the Asp.NET web-api?
- Is there a way to prevent this breakage through attributes, a custom model binder validator, service or similar?
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!
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.
来源:https://stackoverflow.com/questions/11738421/modelbinder-validation-breaks-on-getter-using-reflection