How beautiful the verification looks -- applying verification features and parameter features on Parameters

Source: Internet
Author: User

How beautiful the verification looks -- applying verification features and parameter features on Parameters

If you can apply the feature-based Model verification to parameters,

 

By right-clicking the definition, we can see that RangeAttribute can be applied to parameters,

 

I can see that there is a lot of skin here, so it is easy to implement. If this is the end of Keke, why should I write this article ..

In fact, verification will not be performed in this way, and verification will always pass. Do not believe the picture (there is a picture to see the truth ):

 

So why can I apply it to parameters without verification? Don't worry. Please let me know.

This is the case: model verification is executed when the model is bound, and the verification information is saved in ModelMetadata (see the figure below ), it only contains some model verification and other information related to the model. The features of the parameters we define are parameter information and are not in the model metadata, so there is no verification information we define during verification.

ModelMetadata

 

All the reasons here are clear. To implement feature-based verification and apply it to parameters, simply put the features we have defined into the model metadata.

Step 1: Save the features on the parameter

You can use ParameterDescriptor to obtain the parameter features,

 

To obtain ParameterDescriptor, you can use the GetParameterValue method of ControllerActionInvoker.

We use the GetparameterValue method to save the parameterDescription parameter in the route.

 

We implement a custom ActionInvoker. In the Controller, there is a CreateActionInvoker method used to create ActionInvoker. We rewrite this method to return our custom ActionInvoker,

So far, the parameter information has been saved.

 

Step 2: Generate ModelValidator for the parameter features

We customize a ModelValidatorProvider to provide ModelValidator.

ModelValidator is provided by ModelValidatorProvider. Therefore, I inherit DataAnnotationsModelValidatorProvider. In the rewritten GetValidators method, we extract the previously saved ParameterDescriptor and obtain the attribute and the existing attributes to merge and generate ModelValidator.

At this moment, the parameter features have been generated by ModelValidator and saved in ModelMetadata.

 

Third no: Register ParameterValidationModelValidatorProvider

Remove the existing DataAnnotationsModelValidatorProvider before registering ParameterValidationModelValidatorProvider.

 

In fact, parameter verification still does not work at the moment, because model binding is a model verification and does not validate simple types. So I want to customize a model binding.

Part 4: custom model binding

We inherit the default model binding and override the BindModel method. If it is of a complex type or the model is null, the model is directly returned. We should simply verify this for a simple type. No verification is required.

 

Part 5: instance demonstration,

 

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

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.