简体   繁体   English

如何在ASP.NET Core 2 Web API中仅验证模型状态的一部分

[英]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.

相关问题 如何通过仅允许在 asp.net 核心 web Z8A35DA52ED1057271 的 Model 中分配的属性来验证请求正文 - How to Validate Request Body by allowing only attributes assigned in the Model of an asp.net core web api app 如何验证模型取决于使用ASP.NET Core Web API的布尔数据类型值 - How to validate model depends on bool datatype value using asp.net core web api 如何有条件地验证 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D08 中的整个 model? - How to conditionally validate entire model in ASP.NET Core Web API? 根据Model Asp.net Core API验证Json String - Validate Json String against Model asp.net core api 表单仅发布ASP.NET Core中模型的一部分 - Form post only a part of the model in ASP.NET Core 模型状态在 asp.Net Core web Api 2.1.1 中始终为真 - Model State always true in asp.Net Core web Api 2.1.1 如何在ASP .NET MVC中仅验证部分模型? - How to validate only part of the model in ASP .NET MVC? 如何验证 asp.net 核心 3.0 web api 的获取请求中的参数? - How to Validate parameter in asp.net core 3.0 web api's get request? ASP.NET 核心 Web API - 如何创建用户并在验证期间验证是否存在 - ASP.NET Core Web API - How to create user and validate if not exist during validation ASP.NET 内核 Web API - 如何在 EF 中验证 EndDate 大于 StartDate - ASP.NET Core Web API - How to validate EndDate greater than StartDate in EF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM