In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use ASP.NET to verify the validity of parameters". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The input data of the application should first be verified for validity. The input data can be submitted by the user or other applications. In Web applications, two data validity tests are usually carried out, including client-side verification and server-side verification. The main purpose of the client test is to make the user have a good user experience. First of all, it is best to verify the validity of the form input on the client and show it to the client that the field input is invalid. However, server-side validation is more critical and indispensable (don't just do client-side verification without server-side verification).
Server-side verification is usually performed by the application service (layer), and the methods in the application service (layer) first verify the validity of the data before using the validated data. ABP's infrastructure provides a way to automatically verify the validity of input data.
The application service (layer) method gets a data transfer object (DTO) as input. ABP has an interface for IValidate, and DTO can verify the validity of data by implementing this interface. Since IInputDto extends from IValidate, you can directly implement the IInputDto interface to verify its validity against data transfer objects (DTO).
Use data annotations
ABP provides features for data annotations. Suppose we are developing an application service to create a task and get an input, take a look at the following example:
Public class CreateTaskInput: IInputDto
{
Public int? AssignedPersonId {get; set;}
[Required]
Public string Description {get; set;}
}
Here, the Description attribute is marked Required. AssignedPersonId is optional. There are many such features in the System.ComponentModel.DataAnnotations namespace (for example: MaxLength, MinLength, RegularExpression, and so on).
In the System.ComponentModel.DataAnnotations namespace, see the implementation of Task application service
Public class TaskAppService: ITaskAppService
{
Private readonly ITaskRepository _ taskRepository
Private readonly IPersonRepository _ personRepository
Public TaskAppService (ITaskRepository taskRepository, IPersonRepository personRepository)
{
_ taskRepository = taskRepository
_ personRepository = personRepository
}
Public void CreateTask (CreateTaskInput input)
{
Var task = new Task {Description = input.Description}
If (input.AssignedPersonId.HasValue)
{
Task.AssignedPerson = _ personRepository.Load (input.AssignedPersonId.Value)
}
_ taskRepository.Insert (task)
}
}
As you can see, there is no data validation code written here because ABP automatically verifies the validity of the data. ABP also verifies that the input data is null. If empty, an AbpValidationException exception is thrown. So you don't need to write code to verify that the data is null. It throws the same exception if the input data of any attribute is invalid.
This mechanism is similar to the verification function of ASP.NET MVC. Note: the application service class here is not inherited from Controller, it is a common class used in Web applications.
Custom inspection
If the data annotation method does not meet your needs, you can implement the ICustomValidate interface, as shown in the following example:
Public class CreateTaskInput: IInputDto, ICustomValidate
{
Public int? AssignedPersonId {get; set;}
Public bool SendEmailToAssignedPerson {get; set;}
[Required]
Public string Description {get; set;}
Public void AddValidationErrors (List results)
{
If (SendEmailToAssignedPerson & & (! AssignedPersonId.HasValue) | | AssignedPersonId.Value
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.