asp.net MVC model validation based on annotation features: applying Validationattribute to parameters

Source: Internet
Author: User

asp.net mvc defaults to the model validation mechanism based on the standard features, but only the validationattribute applied on the model type and its properties is valid. If we can apply the validationattribute characteristic directly to the parameter, we not only can realize the simple type (for example int, double and so on) data model verification, but also can realize "one model type, many authentication rules", This article will provide you with the relevant solutions (source code download from here).

First, the validationattribute itself can be applied to the parameters of the

If you are careful, you should find that our common validation features can be applied directly to the parameters of the method. For example, the definition of a AttributeUsageAttribute applied on the type indicates that the target element that can rangeattribute the attribute includes parameters, fields, and properties, as shown below.

   1: [AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property,allowmultiple=false)]
2:public class Rangeattribute:validationattribute
3: {
4: //ellipsis member
5:}

However, for the model validation of ASP.net MVC, the validation features applied to the action method parameters do not work, for the simple reason that the Modelvalidator object used for model validation is created by model metadata based on the parameter type. The validation attribute applied to the parameter itself is not resolved at all.

Second, why the need for model validation based on parameters?

But as I see it, the model validation directly for the action parameter has a high practical significance:

In some cases we cannot modify the data type as model (for example, primitive types such as int, double, and string);

The same model type requires different validation rules in different action method calls.

If we can apply the validation attribute directly to the parameter, the two problems can be solved to some extent.

Third, how to get applied to the parameters of the Validationattribute?

So far, we have a comprehensive understanding of the extensible model Validation system for ASP.net mvc, and now we are extending it so that the validation attributes applied directly to the parameters can take effect. We need to customize a modelvalidatorprovider will provide modelvalidator based on the validation characteristics applied to the parameters, But another problem that needs to be solved before that is how to provide the attributes that are applied to the parameters to our custom Modelvalidatorprovider. Here we present the current controllercontext as the carrier of these characteristics.

The execution of the action method is implemented through Actioninvoker, The default Controlleractioninvoker and Asynccontrolleractioninvoker both define a protected virtual method Getparametervalue based on the Parameterdescriptor object used to describe the parameter and the current CO Ntroller context to bind the corresponding parameter value. Then we can inherit controlleractioninvoker/ Asynccontrolleractioninvoker saves Parameterdescriptor in the current controller context by overriding the method.

For this we have defined a two custom actioninvoker with the following definitions. Parametervalidationactioninvoker and Parametervalidationasyncactioninvoker respectively inherit from Controlleractioninvoker and Asynccontrolleractioni Nvoker. In the overridden Getparametervalue method, we save the Parameterdescriptor object that is a parameter to the current controller context before calling the method with the same name as the base class. Specifically, it is placed in the Datatokens collection of the Routedatadictionary object that represents the current routing data. After the method call, we remove it from the controller context.

1:public class Parametervalidationactioninvoker:controlleractioninvoker
2: {
3:protected Override Object Getparametervalue (ControllerContext controllercontext, Parameterdescriptor ParameterDesc Riptor)
4: {
5:try
6: {
7:controllercontext.routedata.datatokens.add ("Parameterdescriptor", parameterdescriptor);
8:return base. Getparametervalue (ControllerContext, parameterdescriptor);
9:}
10:finally
11: {
12:controllercontext.routedata.datatokens.remove ("Parameterdescriptor");
13:}
14:}
15:}
16:
17:public class Parametervalidationasyncactioninvoker:asynccontrolleractioninvoker
18: {
19:protected Override Object Getparametervalue (ControllerContext controllercontext, Parameterdescriptor parameterDes Criptor)
20: {
21:try
22: {
23:controllercontext.routedata.datatokens.add ("Parameterdescriptor", parameterdescriptor);
24:return base. Getparametervalue (ControllerContext, parameterdescriptor);
25:}
26:finally
27: {
28:controllercontext.routedata.datatokens.remove ("Parameterdescriptor");
29:}
30:}
31:}

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.