After binding all parameters of the action, WEBAPI does not immediately execute the method, but validates the parameter to ensure the legitimacy of the input.
Modelstate
In Apicontroller, a Modelstate property is used to obtain the parameter validation results.
Public abstract class Apicontroller:ihttpcontroller, IDisposable {public modelstatedictionary modelstate {get;} }
The Modelstate property of Apicontext is actually a collection of results for each action parameter validation.
public class modelstatedictionary:idictionary<string, Modelstate>, Icollection<keyvaluepair<string, Modelstate>>, ienumerable<keyvaluepair<string, Modelstate>>, IEnumerable {public int Count {get;} PU blic bool IsReadOnly {get;} public bool IsValid {GET,} public modelstate this[string key] {get, set;} public void Ad D (keyvaluepair<string, modelstate> item); public void Add (string key, Modelstate value); }
public class Modelerror{public String ErrorMessage {get,} public Exception Exception {get;} }
For Modelstatedictionary,modelstate,modelerror we can understand the validation results for all parameters (attributes of all parameters). The validation result of a single parameter. Single validation result for a single parameter property ( One parameter may have multiple difficult results).
The IsValid property of Modelstatedictionary Indicates whether the validation passed. In addition, the Count property may behave differently in WEBAPI for different parameter binding methods. There will be a concrete example behind this.
Validationattribute characteristics
Parameter validation using the Validationattribute feature is the default authentication method for Webapi. Validationattribute is located under the "System.ComponentModel.DataAnnotations" namespace, Some specific validationattribute are also defined under this namespace:
Public abstract class Validationattribute:attribute {public string errormessage {get, set;} public String errormessage resourcename {get; set;} public Type Errormessageresourcetype {get; set;} public virtual bool Requiresvalidationcontex t {get;} public virtual String formaterrormessage (string name); Public Validationresult Getvalidationresult (object value, Validationcontext validationcontext); Public virtual bool IsValid (object value); public void Validate (object value, string name); public void Validate (object value, Validationcontext validationcontext); }
The Validationattribute IsValid method is used to return the validation pass, and the Formaterrormessage method is used to return an error message.
Here I'll customize a arrayvalidatorattribute to make interval judgments, i.e. let the developer customize an array and then determine if the value is within the array.
public class Arrayvalidationattribute:validationattribute {public object[] _list; Public Arrayvalidationattribute (params object[] list) { _list = list, } public object[] list {get {Retu RN _list; } set {_list = value;} } public override bool IsValid (object value) {return _list. Contains (value); } public override string Formaterrormessage (string name) {return string. Format ("{0} is not in interval.", name);} }
Use of Customervalidationattribute
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method | Attributetargets.property | Attributetargets.field | Attributetargets.parameter, AllowMultiple = true)] public sealed class Customvalidationattribute:validationattribute { Public Customvalidationattribute (Type Validatortype, string method); public string Method {get;} public override Object TypeId {get,} public Type Validatortype {get;} public override St Ring Formaterrormessage (string name); protected override Validationresult IsValid (object value, Validationcontext validationcontext); }
From the definition we can see that Customervalidationattribute is not inherited, This is a description of how Customervalidationattribute differs from the use of validaitionattribute. In fact, the customervalidationattribute is used in the way of reflection to verify. It contains the Validationtyp E,method two attributes that represent the class and method name to which the reflection is used, respectively.
The following conditions are true for a method that can be used as a validation:
- The method is public, static
- Method must have one or two parameters.
- If the method has only one parameter, then this parameter must be ref,out callout (the parameter is actually the value to verify)
- If the method has two parameters, then the first parameter is constrained by 3, the second argument must be the Validationcontext type
Model binding vs. deserialization parameter results
The model binding method saves all validation results, and the deserialization method only saves the results of the validation failure.
As follows:
Public Modelstatedictionary postfigurefromurlforstate ([modelbinder]figure figure) {return modelstate; } Public Modelstatedictionary postfigurefrombodyforstate ([frombody]figure figure) {return modelstate;
For figure, set the lastname,firstname to be required.
public class Figure { [Required] public string FirstName {get; set;}
Now we pass the same data to two methods: {firstname:null,lastname: "Stack"}
Url:
/api/demo/postfigurefromurlforstate? Lastname=stack
/api/demo/postfigurefrombodyforstate
The actual results were as follows:
Source
Github:https://github.com/barlowdu/webapi (Api_11)
ASP. WebAPI 11 Parameter Validation