How beautiful this verification looks--applying validation attributes on parameters

Source: Internet
Author: User

If you can apply feature-based Model validation to parameters,

By right-clicking on the definition, you can see clearly that rangeattribute can be applied to the parameters,

See here there is a very good wood, so simple is to achieve, cough cough if this is the end, then I write this article why, hehe.

In fact, this is not to perform validation, verification is always passed, do not believe the picture (there is a picture to the truth):

That's why it can be applied to parameters, but does it not perform validation? Don't worry, please listen to me slowly.

This is true: Model validation is performed at the time the model is bound, and the validation information is stored in the Modelmetadata (see figure below), which contains only some validation of the model and other information related to the model, and the attributes of the parameters we define are parameter information, not in the model metadata, So there is no feature validation information that we define at the time of verification.

Modelmetadata

The reason for all this here is clear, to realize the application of feature-based validation in the parameters is simply to put our defined features into the model metadata on the line.

The first step: Save the attributes on the parameters

To obtain the parameter characteristics can be obtained through the parameterdescriptor,

To get Parameterdescriptor can be obtained by Controlleractioninvoker's Getparametervalue method.

We save the parameter Parameterdescription in the route through the Getparametervalue method.

We are implementing a custom actioninvoker. there is a Createactioninvoker method in the Controller that is used to create the Actioninvoker, and we override this method to return to our custom actioninvoker,

The information for this parameter has been saved by us.

The second step: the characteristic of the parameter is generated Modelvalidator

We customize a modelvalidatorprovider to provide modelvalidator

Modelvalidator is provided by Modelvalidatorprovider, so I inherited the Dataannotationsmodelvalidatorprovider, in the rewritten getvalidators method, we extract the previously saved parameterdescriptor, get attribute and the existing attributes merge, Generate Modelvalidator.

Now the attributes on the parameter have been generated modelvalidator and saved in Modelmetadata

Third not: Register Parametervalidationmodelvalidatorprovider

Before registering the parametervalidationmodelvalidatorprovider , you need to add the existing Dataannotationsmodelvalidatorprovider removal.

Parameter validation is still not effective at this point because model binding is a model validation that does not enforce validation on simple types. So I'm going to customize a model binding.

Part fourth: Custom model Bindings

We inherited the default model bindings, overriding the Bindmodel method, and if it is a complex type or model is null, return directly Model, should be for us this is just for simple type validation. There is no need to verify the space.

The fifth part: example demonstration,

PS: Thanks to Jing Jinnan Teacher's blog http://www.cnblogs.com/artech/archive/2012/06/11/data-annotations-model-validation-04.html

How beautiful this verification looks--applying validation attributes on parameters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.