[英]How to validate only a part of Model State in asp.net core 2 web api
We are using a Usermaster DTO in 2 different projects. 我们正在2个不同的项目中使用Usermaster DTO。
public class UserMaster : BaseProperties
{
[BsonElement]
[BsonRequired]
[Required]
public string FirstName { get; set; }
[BsonElement]
[BsonRequired]
[Required]
public string LastName { get; set; }
[BsonElement]
[BsonRequired]
[EmailAddress]
[Required]
public string EmailId { get; set; }
[BsonElement]
[BsonRequired]
[DataType(DataType.Password)]
[Required]
public string Password { get; set; }
[BsonIgnore]
[DataType(DataType.Password)]
[Required]
public string NewPassword { get; set; }
}
Now we are using this DTO in 2 different projects. 现在,我们正在2个不同的项目中使用此DTO。
Project 1 is Admin in asp.net MVC 5.0, There we can use ModelState.IsValidField to validate only a part of this whole Model. 项目1是asp.net MVC 5.0中的Admin,可以使用ModelState.IsValidField来验证整个模型的一部分。
Project 2 is a Web Api build in asp.net core 2. There i could not find any solution which can only validate Email and Password for login purpose.. 项目2是在asp.net核心2中构建的Web Api。在那里,我找不到任何只能验证电子邮件和密码以用于登录目的的解决方案。
Basically i am facing issue in asp.net core 2 web api where i could not specify the exact data member that could be only validate. 基本上,我在asp.net core 2 web api中遇到问题,我无法指定仅可以验证的确切数据成员。 I have to pass anything on other [Required] fields to validate request.
我必须在其他[必填]字段中传递任何内容以验证请求。 likewise ModelState.IsValidField
同样是ModelState.IsValidField
Any solutions?? 任何解决方案?
If you have two different validation requirements, then you should have two different view models/DTOs. 如果您有两个不同的验证要求,则应该有两个不同的视图模型/ DTO。 The whole entire point of a view model/DTO is handle particular usage scenario.
视图模型/ DTO的整个要点是处理特定的使用场景。 Here, you have two different sets of request data, so your issue is entirely down to trying to use the same class to satisfy both, when the two are not the same.
在这里,您有两组不同的请求数据,因此,当两者不相同时,您的问题完全取决于尝试使用相同的类来满足两者。
If you want to reduce code duplication, simply continue using inheritance: 如果要减少代码重复,只需继续使用继承:
public class UserLogin : BaseProperties
{
[BsonElement]
[BsonRequired]
[EmailAddress]
[Required]
public string EmailId { get; set; }
[BsonElement]
[BsonRequired]
[DataType(DataType.Password)]
[Required]
public string Password { get; set; }
}
public class UserMaster : UserLogin
{
[BsonElement]
[BsonRequired]
[Required]
public string FirstName { get; set; }
[BsonElement]
[BsonRequired]
[Required]
public string LastName { get; set; }
[BsonIgnore]
[DataType(DataType.Password)]
[Required]
public string NewPassword { get; set; }
}
in your DTO implement IValidatableObject 在您的DTO中实现IValidatableObject
public class UserMaster : BaseProperties, IValidatableObject
{
[BsonElement]
[BsonRequired]
[Required]
public string FirstName { get; set; }
[BsonElement]
[BsonRequired]
[Required]
public string LastName { get; set; }
[BsonElement]
[BsonRequired]
[EmailAddress]
[Required]
public string EmailId { get; set; }
[BsonElement]
[BsonRequired]
[DataType(DataType.Password)]
[Required]
public string Password { get; set; }
[BsonIgnore]
[DataType(DataType.Password)]
[Required]
public string NewPassword { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
//you custom validation here...
}
}
So if you can detect the context it is being used in then you are on your way... 因此,如果您可以检测到上下文正在被使用,那么您就可以了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.