In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what are the verification programming methods under ASP.NET MVC, which can be used for reference by interested friends. I hope you will gain a lot after reading this article.
1. ModelValidator and ModelValidatorProvider
Although the way Model binds varies depending on the type of data being validated, ASP.NET MVC always uses an object called ModelValidator to validate bound data objects. All ModelValidator types inherit from the abstract class ModelValidator with the following definition. Its GetClientValidationRules method returns a collection of element type ModelClientValidationRule, and ModelClientValidationRule is an encapsulation of client-side validation rules, which we will describe in more detail in the client-side validation section.
Public abstract class ModelValidator {/ / other members public virtual IEnumerable GetClientValidationRules (); public abstract IEnumerable Validate (object container); public virtual bool IsRequired {get;}}
The validation of the target data is done by calling the Validate method, whose input parameter container represents the object being validated. It is precisely because the object being validated is always a complex type object, which is also called a "container" object with several data members, so the corresponding parameter is named container. The Validate method indicates that the return value of the validation result is not a simple Boolean value, but an element type of a collection of ModelValidationResult objects with the following definition.
Public class ModelValidationResult {public string MemberName {get; set;} public string Message {get; set;}}
ModelValidationResult has two string type properties, MemberName, which represents the name of the validated data member, and Message, which represents an error message. In general, if the ModelValidationResult object comes from validation against the container object itself, its MemberName property is an empty string. For validation against a property of the container object, the property name is used as the MemberName property of the returned ModelValidationResult object.
The ModelValidationResult collection is returned only if validation fails. If the validated data object conforms to all the validation rules, the Validate method directly returns Null or an empty ModelValidationResult collection. It is worth mentioning that we sometimes use ValidationResult's static read-only field Success to represent the result of successful validation, which in fact has a value of Null.
Public class ValidationResult {/ / other members public static readonly ValidationResult Success;}
ModelValidator has a read-only property of Boolean type IsRequired indicating whether the ModelValidator validates the target data "required" (that is, the validated data member must have a specific value), which returns False by default. We can define a property as a "required" data member by applying the RequiredAttribute attribute.
We know that most of the ASP.NET MVC uses the Provider schema to provide the corresponding components. For example, the ModelMetadata describing the Model metadata is provided through the corresponding ModelMetadataProvider, and the ModelBinder for Model binding can be provided through the corresponding ModelBinderProvider. The ModelValidator used to implement Model verification is no exception, its corresponding provider is ModelValidatorProvider, and the corresponding type inherits from the abstract class ModelValidatorProvider defined below.
Public abstract class ModelValidatorProvider {public abstract IEnumerable GetValidators (ModelMetadata metadata, ControllerContext context);}
As shown in the code snippet above, the GetValidators method has two parameters, one is the ModelMetadata object that describes the validated type or property Model metadata, and the other is the current ControllerContext. This method returns a collection of element type ModelValidator.
ASP.NET MVC registers the ModelValidatorProvider used with the static type ModelValidatorProviders. As shown in the following code snippet, ModelValidatorProviders has a static read-only property Providers with the corresponding type ModelValidatorProviderCollection, which represents a global ModelValidatorProvider collection based on the entire scope of Web application.
Public static class ModelValidatorProviders {public static ModelValidatorProviderCollection Providers {get;}} public class ModelValidatorProviderCollection: Collection {public ModelValidatorProviderCollection (); public ModelValidatorProviderCollection (IList list); public IEnumerable GetValidators (ModelMetadata metadata, ControllerContext context);}
It is worth mentioning that the ModelMetadata type used to describe Model metadata has the following GetValidators method, which returns a ModelValidator list that is created using the ModelValidatorProvider registered on the ModelValidatorProviders static property Providers.
Public class ModelMetadata {/ / other members public virtual IEnumerable GetValidators (ControllerContext context);}
The UML shown in the figure lists the three core types that make up the Model authentication system. Specific Model validation is always done through a specific ModelValidator, and ModelValidatorProvider as an ModelValidator provider is registered on top of the static type ModelValidatorProviders.
II. DataAnnotationsModelValidator
We introduce three different "automatic verification" programming methods in "four ways of validation programming under ASP.NET MVC". ASP.NET MVC internally uses different ModelValidator to validate the bound parameters. A specific ModelValidator usually has a corresponding ModelValidatorProvider to provide, and the native ModelValidator and corresponding ModelValidatorProvider provided by ASP.NET MVC will be described in detail in the following content.
Of the three validation programming methods mentioned above, the first (using the ValidationAttribute feature applied to a data type or its data members to define the corresponding validation rules) is the most common. The declarative verification solution based on the ValidationAttribute feature is finally accomplished through DataAnnotationsModelValidator. A DataAnnotationsModelValidator object is actually an encapsulation of a ValidationAttribute feature, as can be seen from the definition shown below.
Public class DataAnnotationsModelValidator: ModelValidator {public DataAnnotationsModelValidator (ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute); public override IEnumerable GetClientValidationRules (); public override IEnumerable Validate (object container); protected internal ValidationAttribute Attribute {get;} protected internal string ErrorMessage {get;} public override bool IsRequired {get;}}
The provider of DataAnnotationsModelValidator is DataAnnotationsModelValidatorProvider, and details on ValidationAttribute, DataAnnotationsModelValidator, and DataAnnotationsModelValidatorProvider can be found in the previous three articles.
Model Verification of ASP.NET MVC based on Dimension Properties: ValidationAttribute
Model Verification of ASP.NET MVC based on Dimension Properties: DataAnnotationsModelValidator
Model Verification of ASP.NET MVC based on Dimension Properties: DataAnnotationsModelValidatorProvider
III. ValidatableObjectAdapter
If the validated data type implements the IValidatable interface, ASP.NET MVC automatically calls the implemented Validate method to verify it, and the ModelValidator created at this time is a ValidatableObjectAdapter object. ValidatableObjectAdapter is defined as follows, and the implementation logic of its Validate method is simple: it directly calls the Validate method of the validated object and converts the returned ValidationResult object to a ModelValidationResult type.
Public class ValidatableObjectAdapter: ModelValidator {public ValidatableObjectAdapter (ModelMetadata metadata, ControllerContext context); public override IEnumerable Validate (object container);}
Although ValidatableObjectAdapter inherits from ModelValidator, ASP.NET MVC doesn't seem to see it as a real ModelValidator, but as an "Adapter". ASP.NET MVC also does not define a separate ModelValidatorProvider for ValidatableObjectAdapter, but its provider is actually the DataAnnotationsModelValidatorProvider mentioned above.
IV. DataErrorInfoModelValidator
If we let the data type implement the IDataErrorInfo interface, we can use the implemented Error properties and indexes to provide validation error information for ourselves and the data members to which we belong. For such a data type, ASP.NET MVC eventually creates a DataErrorInfoModelValidator object to validate it, and DataErrorInfoClassModelValidator and DataErrorInfoPropertyModelValidator are two specific DataErrorInfoModelValidator.
DataErrorInfoClassModelValidator and DataErrorInfoPropertyModelValidator are two internal types. The former implements validation against the container object itself, so it only needs to extract the error message from the implemented Error property and convert it into a returned ModelValidationResult object. The latter specifically validates a property of the container object, which uses the property name in the implemented Validate method to extract the corresponding error message from the implemented index and convert it into a returned ModelValidationResult object.
Internal sealed class DataErrorInfoClassModelValidator: ModelValidator {public DataErrorInfoClassModelValidator (ModelMetadata metadata, ControllerContext controllerContext); public override IEnumerable Validate (object container);} internal sealed class DataErrorInfoPropertyModelValidator: ModelValidator {public DataErrorInfoPropertyModelValidator (ModelMetadata metadata, ControllerContext controllerContext); public override IEnumerable Validate (object container);}
ASP.NET MVC ends up using DataErrorInfoModelValidatorProvider with the following definition to provide these two types of DataErrorInfoModelValidator. For the GetValidators method it implements, if the type of the object being validated implements the IDataErrorInfo interface, it creates a DataErrorInfoClassModelValidator object and adds it to the returned ModelValidator list. If a property value of the container type is validated and the container type implements the IDataErrorInfo interface, it creates a DataErrorInfoPropertyModelValidator object and adds it to the returned ModelValidator list.
Public class DataErrorInfoModelValidatorProvider: ModelValidatorProvider {public override IEnumerable GetValidators (ModelMetadata metadata, ControllerContext context);} Thank you for reading this article carefully. I hope the article "what are the verification programming methods under ASP.NET MVC" shared by the editor will be helpful to you? at the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.