RequiredIf 构造函数重载 .net core 3.1 的问题

Problem with RequiredIf constructor overloading .net core 3.1

我正在为我的一个项目尝试在 .net 核心中使用 requiredif 验证器。 我有 2 个构造函数用于 requiredif 属性

public RequiredIfAttribute(string s1,string s2,string errormsg=""){}

还有

public RequiredIfAttribute(string s1,string s2,string s3,string errormsg=""){}

当使用以下代码时,对于以下两种情况,仅调用具有 3 个参数的第一个构造函数 [RequiredIf("test","test","test","test")], [RequiredIf("test","test","test")]

有谁知道当在 RequiredIf 中传递 4 个参数时如何使用 4 个参数实际命中该构造函数

代码

public class model
   {
     [RequiredIf("test","test","test",ErrorMessage="test")]
     public string content{get:set;}
     [RequiredIf("test","test",ErrorMessage="test")]
      public string version{get;set;}
   } 


public classs RequiredIfAttribute:ValidationAttribute
{
public RequiredIfAttribute(string s1,string s2,string errormsg=""){}

public RequiredIfAttribute(string s1,string s2,string s3,string errormsg=""){}
}
     

查看下面我的示例代码,它在我这边有效。

using System.ComponentModel.DataAnnotations;

namespace WebApplication1.Util
{
    public class RequiredIfAttribute : ValidationAttribute
    {
        private readonly string _s1;
        private readonly string _s2;
        private readonly string _s3;
        private readonly string _errormsg;
        public RequiredIfAttribute(string s1, string s2, string errormsg = "") {
            _s1 = s1;
            _s2 = s2;
            _errormsg = errormsg;
        }
        public RequiredIfAttribute(string s1, string s2, string s3, string errormsg = "") {
            _s1 = s1;
            _s2 = s2;
            _s3 = s3;
            _errormsg = errormsg;
        }

        public override bool IsValid(object value)
        {
            if (_s3 == null)
            {
                var inputValue = (string)value;
                if (_s1 == "test" && _s2 == "test")
                {
                    return false;
                }
                return true;
            }
            else {
                if (_s1 == "test" && _s2 == "test" && _s3 == "test")
                {
                    return false;
                }
                return true;
            }
            
        }

        public override string FormatErrorMessage(string name)
        {
            return _errormsg;
        }
    }
}

我的测试模型

using System.ComponentModel.DataAnnotations;
using WebApplication1.Util;

namespace WebApplication1.Models
{
    public class User
    {
        [RequiredIf("test","test","errorMsg")]
        public string user_desc { get; set; }
        [Required(ErrorMessage = "age is necessary")]
        public string age { get; set; }
        [RequiredIf("test", "test", "test", "errorMsg for three param")]
        public string content { get; set; }
    }
}

我的控制器:

public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create([Bind("user_desc,age,content")] User user)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View(user);
        }

我的观点

@model WebApplication1.Models.User
@{

}

<h4>User</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="user_desc" class="control-label"></label>
                <input asp-for="user_desc" class="form-control" />
                <span asp-validation-for="user_desc" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="age" class="control-label"></label>
                <input asp-for="age" class="form-control" />
                <span asp-validation-for="age" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="content" class="control-label"></label>
                <input asp-for="content" class="form-control" />
                <span asp-validation-for="content" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

这是测试结果。