This article is a mirror article of machine translation, please click here to jump to the original article.

View: 45361|Reply: 1

[ASP.NET] ASP.NET Core(4) filter unified ModelState model validation

[Copy link]
Posted on 2021-3-13 20:48:05 | | | |
Model state and data annotations in C# provide us with a convenient way to request data verification. ModelState model verification simply means to help the backend save a lot of parameter verification work, just add corresponding features to the model, and finally call ModelState.IsValid in the controller to get the verification results. Without ModelState, our backend parameter validation may need to write a lot of if judgments.

Requirements: We judge whether the parameters of POST have passed the model verification in the filter, if they do not pass the verification, directly return a unified json error message, only after the verification passes, and then enter the controller method, the advantage of this is,We don't need to judge each controller and do repetitive work

Review the ModelState model validation

webapi ModelState.IsValid model validation
https://www.itsvse.com/thread-3809-1-1.html

Manually validate model fields ASP.NET MVC
https://www.itsvse.com/thread-9500-1-1.html

Asp.net Core Model Data Validation
https://www.itsvse.com/thread-9016-1-1.html

Review filters

mvc custom filter ActionFilterAttribute class

https://www.itsvse.com/thread-2609-1-1.html

Review your knowledge of ASP.NET Core

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html

The system predefines the ValidationAttribute
A set of validation features that inherit from the ValidationAttribute are defined under the System.ComponentModel.DataAnnotations namespace.

  • RequiredAttribute: Used to validate required data fields.
  • RangeAttribute: Used to verify that the value of a numeric field is within the specified range.
  • StringLengthAttribute: Used to verify that the string length of the target field is within the specified range.
  • MaxLengthAttribute/MinLengthAttribute: Used to verify whether the length of the character/array dictionary is less than/greater than the specified upper/lower limit.
  • RegularExpressionAttribute: Used to verify that the format of the string field matches the specified regular expression.
  • CompareAttribute: Used to verify whether the value of the target field is consistent with the value of another field, and can be used to confirm the consistency of entering the password twice in a user registration scenario.
  • CustomValidationAttribute: Specifies a validation type and validation method to validate the target member.



Of course, we can also define the rules for validation by inheriting the ValidationAttribute class and implementing the corresponding method.

Let's take a look at the renderings first, the error message is returned by the background interface, as follows:



Object Model Code:

ModelStateFilter filter code:



The IsAjaxRequest code is as follows:

Finally, add a custom filter to the Startup file, the code is as follows:



(End)




Previous:HtmlAgilityPack parses HTML tips for rollups
Next:.NET Core calls Baidu PaddleOCR to recognize images and texts
Posted on 2021-9-22 20:40:25 |
Learn to learn...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com