Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用RegularExpression注释验证模型_C#_.net_Regex_Asp.net Web Api_Data Annotations - Fatal编程技术网

C# 使用RegularExpression注释验证模型

C# 使用RegularExpression注释验证模型,c#,.net,regex,asp.net-web-api,data-annotations,C#,.net,Regex,Asp.net Web Api,Data Annotations,我在WebAPI项目中添加了一个过滤器,以验证从客户端传递的所有模型。为了验证模型,我使用了数据注释。除了我使用RegularExpression注释外,它似乎都可以正常工作 这是我在api中使用的过滤器: public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) {

我在WebAPI项目中添加了一个过滤器,以验证从客户端传递的所有模型。为了验证模型,我使用了数据注释。除了我使用RegularExpression注释外,它似乎都可以正常工作

这是我在api中使用的过滤器:

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid == false)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, actionContext.ModelState);
        }
    }
这是未正确验证的模型属性:

    [Required]
    [MinLength(8)]
    [StringLength(255)]
    [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)", ErrorMessage = "Password must contain at least one capital letter and one number")]
    public string Password { get; set; }
无论我在字符串中传递什么值,我都会不断得到一个错误。如果有任何帮助,我们将不胜感激。

试试这个:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$
Prob密码格式正确时,表达式返回null