如何在ASP.NET Core 2 Web API中仅验证模型状态的一部分 [英] How to validate only a part of Model State in asp.net core 2 web api

查看:71
本文介绍了如何在ASP.NET Core 2 Web API中仅验证模型状态的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在2个不同的项目中使用Usermaster DTO.

We are using a Usermaster DTO in 2 different projects.

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; }        
}

现在,我们正在2个不同的项目中使用此DTO.
项目1 是asp.net MVC 5.0中的Admin,可以在其中使用ModelState.IsValidField来验证整个模型的一部分.
项目2 是asp.net核心2中的Web Api构建.在那里,我找不到任何只能验证电子邮件和密码以用于登录目的的解决方案.

基本上,我在asp.net core 2 Web api中遇到问题,我无法指定仅可以验证的确切数据成员.我必须在其他[必填]字段中传递任何内容以验证请求.同样是ModelState.IsValidField

任何解决方案?

Now we are using this DTO in 2 different projects.
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.
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..

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. I have to pass anything on other [Required] fields to validate request. likewise ModelState.IsValidField

Any solutions??

推荐答案

如果您有两个不同的验证要求,则应该有两个不同的视图模型/DTO.视图模型/DTO的整个要点是处理特定的使用场景.在这里,您有两组不同的请求数据,因此,当两者不相同时,您的问题完全取决于尝试使用相同的类来满足两者.

If you have two different validation requirements, then you should have two different view models/DTOs. The whole entire point of a view model/DTO is handle particular usage scenario. 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; }        
}

这篇关于如何在ASP.NET Core 2 Web API中仅验证模型状态的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆